Greasemonkey Script Version Constant - version

I defined the version of my user script in the meta block, like this:
// ==UserScript==
// #name Script Name
// #description Something about what this script does
// #include http://www.example.com/
// #version 5.3.0
// #run-at document-end
// ==/UserScript==
Is there a way to get the version number that I defined? I want to be able to do something like alert("This is version " + SCRIPT_VERSION + ".");.

If you upgrade to Greasemonkey 0.9.16 (just released), you can use the brand new GM_info object.
You can add this to your script example, above:
var myVersion = GM_info.script.version;
console.log ('Version: ', myVersion, myVersion === "5.3.0");
Which would output this to the console:
Version: 5.3.0 true
For GM versions prior to 0.9.16, you'd have to either read your own script in as a #resource or use encapsulation techniques as shown in "Knowing Your Own Metadata".

Related

How Do I Make A Tampermonkey Userscript Replace Words In Text Files?

6 years ago I asked a question about rewriting text files displayed in a browser using greasemonkey.
Can I Make Greasmonkey Scripts Run On Text Files?
I am now coming back to a similar problem and I tried to paste it in to Tampermonkey but it doesn't replace the text.
What am I doing wrong here?
// ==UserScript==
// #name Rewrite LLVM License
// #namespace http://tampermonkey.net/
// #version 0.1
// #match http://llvm.org/releases/2.8/*
// #include http://llvm.org/releases/2.8/LICENSE.TXT
// #require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// #grant none
// ==/UserScript==
(function() {
//Just to tell the linter that $ is defined in jquery
/* global $ */
//Browsers display text in a pre tag
var pageTextNd=$("body > pre");
//Replace the text LLVM
var newPageTxt=pageTextNd.text().replace("LLVM", "Ernst Blofeld");
//Rewrite the page
pageTextNd.text(newPageTxt);
})();
It looks like the page you're interested in redirects to:
https://releases.llvm.org/2.8/LICENSE.TXT
so that's what you need to set your #include or #match to.
You also want to replace all instances of LLVM, so use .replaceAll:
// ==UserScript==
// #name Rewrite LLVM License
// #include https://releases.llvm.org/2.8/LICENSE.TXT
// #require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// #grant none
// ==/UserScript==
(function() {
//Just to tell the linter that $ is defined in jquery
/* global $ */
//Browsers display text in a pre tag
var pageTextNd=$("body > pre");
//Replace the text LLVM
var newPageTxt=pageTextNd.text().replaceAll("LLVM", "Ernst Blofeld");
//Rewrite the page
pageTextNd.text(newPageTxt);
})();
If you don't want to rely on replaceAll, use a regular expression with .replace instead: /LLVM/g.
It seems quite strange to rely on jQuery for something this trivial though - you can very easily accomplish this without a library:
// ==UserScript==
// #name Rewrite LLVM License
// #include https://releases.llvm.org/2.8/LICENSE.TXT
// #grant none
// ==/UserScript==
const pre = document.querySelector('body > pre');
pre.textContent = pre.textContent.replaceAll('LLVM', "Ernst Blofeld");

Tampermonkey script not auto-updating

At the top of my script I have the following:
// ==UserScript==
// #name Test script
// #description testing auto-update
// #namespace http://tampermonkey.net/
// #author newbie
// #version 1.0.0
// #updateURL https://github.com/mygithubaccount/test/raw/master/test.user.js
// #downloadURL https://github.com/mygithubaccount/test/raw/master/test.user.js
// #match http://awebsite.com/*
// #run-at document-end
// #grant GM_getResourceText
// #grant GM_addStyle
// #grant GM_xmlhttpRequest
// #grant GM_getResourceURL
// #grant GM_xmlhttpRequest
// ==/UserScript==
Please note that these values are just for example.
When I make changes on the script and increase the version number on github and then push the changes to master it updates the raw script link, however I don't get auto-updates from Tampermonkey as in popups saying the script has an update. It will only update if I manually go to the link and reinstall the script to update it.
How can I make this auto-update with popups?
I am not an expert at userscripts, can only tell from my experience making TamperMonkey work:
In order for TamperMonkey to update, I had to copy&paste the URL into Update URL: on the script's Settings tab:
(Make sure ☒ Check for updates is turned on, obviously.)
Then if you manually Check for userscript updates on the TamperMonkey icon, you should see a proper popup.

Tampermonkey script not updating

I am having issues getting my script to auto-update. If I force a check for an update, then it picks up the new version just fine. However, I am unable to get this to automatically notify me whenever a new update is available.
My script has the following metadata:
// ==UserScript==
// #name PortalScript
// #namespace PortalScript
// #version 2.1.0.9
// #description Contains customizations to be applied to the portal
// #author Tristan Lee
// #match https://test-support.portal.dev
// #grant none
// #downloadURL https://rawgit.com/tristanlee85/supportportal/master/supportportal.js
// #updateURL https://rawgit.com/tristanlee85/supportportal/master/supportportal.js
// ==/UserScript==
Tampermonkey is set to check for updates every hour. However, the version in the repository may be 2.1.0.10, but after hours since the version change, I am never notified that an update is available.
I also don't believe #updateURL is necessary here since #downloadURL is specified, but I added it as a check since only having #downloadURL didn't work either.

Replace HTML JS and CSS files with tampermokey

Is it possible to replace HTML, JS and CSS files with Tampermonkey?
These files would be hosted on a server and would just replace the files I want like the index.html, a JS files and the main styles CSS.
I could only find how to replace functions of a JS files but not how to replace a file...
This is the only thing i found: (but it's not working)
// ==UserScript==
// #name New Userscript
// #namespace http://tampermonkey.net/
// #version 0.1
// #description try to take over the world!
// #author You
// #match http://xxx.xx/
// #grant none
// ==/UserScript==
for (var i = document.styleSheets.length - 1; i >= 0; i--) {
document.styleSheets[i].disabled = true;
}
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'http://xx.com/xx/x/x.x.x/xxxx.css';
document.getElementsByTagName("head")[0].appendChild(link);
edit: this works for the css file...but the index.html is not linking to it...
Maybe its not possible :)
Thanks for any help.
(http://www.htmlgoodies.com/beyond/javascript/javascript-dynamic-document-creation-in-new-windows.html)
Try looking at the link and check out (in order) the topics "Cross-Writing Variables" and "Cross-Window HTML." If you do that, you'll understand more about what tampermonkey is doing within the script snippet you're using above and also you should be able to see how to set your existing html document to the default html document that loads.
One side note, there's better ways to perform this process if you have access to install serving languages or frameworks to your server.
If you are using Chromium with extension support check out Resource Override. It does what you want. JS, CSS, HTML, also Modifying Response headers. Can redirected to another URL whether it's remote or localhost, or store the code directly in the plugin.

Tampermonkey script that runs only in incognito?

Is it possible to make any script into a script that only runs on www.example.com, Only if the website is being accessed from an incognito window? (chrome)
I've added a isIncognito flag to Tampermonkey's GM_info. So you now can check the incognito mode like this:
// ==UserScript==
// #name testIncognito
// #namespace http://tampermonkey.net/
// #version 0.1
// #description enter something useful
// #match http://*/*
// #copyright 2012+, You
// ==/UserScript==
if (GM_info.isIncognito) {
alert([ GM_info.scriptHandler, 'detected incognito mode #', window.location.href ].join(' '));
}
Please not that this at the moment only is available at TM beta version 3.0.3353 and higher.

Resources