Table of Contents Previous entry (api:GM_audio.removeStateChangeListener) Next entry (api:CDATA)
If a script runs on a single-page application, then it can use window.onurlchange to listen for URL changes:
// ==UserScript==
...
// @grant window.onurlchange
// ==/UserScript==
if (window.onurlchange === null) {
// feature is supported
window.addEventListener('urlchange', (info) => ...);
}
Usually JavaScript is not allowed to close tabs via window.close.
Userscripts, however, can do this if the permission is requested via @grant.
Note: for security reasons it is not allowed to close the last tab of a window.
// ==UserScript==
...
// @grant window.close
// ==/UserScript==
if (condition) {
window.close();
}
window.focus brings the window to the front, while unsafeWindow.focus may fail due to user settings.
// ==UserScript==
...
// @grant window.focus
// ==/UserScript==
if (condition) {
window.focus();
}