Dose Tampermonkey can run script before the page loaded? - tampermonkey

I'm trying to use tampermonkey to refresh a website, its works except the server was busy, and the respond time is long, may i know can the script run before the page fully loaded?
I would also wanna now can the script work on any website without the #match / #include?
Many thanks.
// #name TEST
// #version 1
// #grant none
// #match http://busy.example.com
// ==/UserScript==
setTimeout(
function(){
document.location = "http://example.com";
}, 10
);

Related

Is there a way to allow Tampermonkey to run on the chrome homepage?

I am trying to make an extension using the right click menu. I noticed that the menu didn't display on the Chrome homepage because Tampermonkey didn't run on it. This is my current code:
// ==UserScript==
// #name Go to Website.Net
// #namespace http://tampermonkey.net/
// #description Context menu to execute UserScript
// #version 0.1
// #author author
// #include *
// #grant GM_openInTab
// #run-at context-menu
// ==/UserScript==]
(function() {
'use strict';
GM_openInTab("https://website.net");
})();
Is there a way to allow Tampermonkey to run on the chrome homepage?

Why does a tampermonkey script for https://news.google.com/* appear in the menu for https://www.nytimes.com/?

I have a tampermonkey script with this header:
// ==UserScript==
// #name Google News - Remove old articles
// #namespace http://tampermonkey.net/
// #version 0.2
// #description Remove articles that already appeared recently
// #author AC
// #require http://code.jquery.com/jquery-latest.js
// #match https://news.google.com/*
// #icon https://www.google.com/s2/favicons?domain=google.com
// #grant none
// ==/UserScript==
When I am on the page https://www.nytimes.com/ and I click on the tampermonkey icon in the Firefox toolbar, this script appears. Why?
Because the page contains an iframe, and that iframe links to news.google.com:
Userscripts run in iframes, and when they do, they get added to the Tampermonkey icon at the top.

How do I take text contained in web page and make it part of the page title?

I desperately need help with writing a Tampermonkey/Greasemonkey script that takes part of the information within the web page and makes it part of the page (and window) title.
A client's name is part of the target (internal) web page, and clearly labeled within the HTML:
<div id="patient-info" class="ehr-patients-info">
<div id="patient-identification">
<span title="" id="patient-name">
Johnnyfirst
Smithylast
</span>
</div>
...
I want to add the text "Johnnyfirst Smithylast" to the window title and tried:
var ptname = document.getElementById("patient-name") ;
document.title = document.title + " | Name: " + ptname ;
But that resulted in titles like: ...| Name: null.
The second problem is that the web site to which I am piggybacking this userscript doesn't load all at once. After the initial page load, there's heavy javascript functionality which loads various parts of the page and ends up displaying the client name as above.
When I try $(window).on('load', function() { ... }) or $(document).ready(), it seems to be acting on a preliminary version of the web page that doesn't have the information fully loaded yet.
Your target page is AJAX driven and Greasemonkey/Tampermonkey fires way before most AJAX page loads finish. So, you must use techniques like MutationObserver, waitForKeyElements, etc., to compensate.
For example, here's a complete Tampermonkey script that changes the title when it finds the patient-name node:
// ==UserScript==
// #name _Put the patient Name in the title
// #match *://YOUR_SERVER.COM/YOUR_PATH/*
// #noframes
// #require https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// #require https://gist.github.com/raw/2625891/waitForKeyElements.js
// #grant GM_addStyle
// #grant GM.getValue
// ==/UserScript==
// #grant none
//- The #grant directives are needed to restore the proper sandbox.
/* global waitForKeyElements */
/* eslint-disable no-multi-spaces, curly */
'use strict';
waitForKeyElements ("#patient-name, .patient-name", scrapeTextToTitle);
function scrapeTextToTitle (jNode) {
var nameRaw = jNode.text ().trim ();
var nameSanitized = nameRaw.replace (/\s+/g, " ");
document.title += " | Name: " + nameSanitized;
}

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 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