Tampermonkey / tampermonkey

Tampermonkey is the most popular userscript manager, with over 10 million users. It's available for Chrome, Microsoft Edge, Safari, Opera Next, and Firefox.
GNU General Public License v3.0
4.29k stars 424 forks source link

@match works only if page is reloaded or opened in new tab/window #1897

Closed kyxap closed 12 months ago

kyxap commented 12 months ago

Steps to reproduce

0) Pit Tampermonkey plugin to your browser Tool Bar for visibility 1) Open https://www.linkedin.com/jobs/ 2) Click on Show All (you will be redirected to https://www.linkedin.com/jobs/collections/recommended/*) 3) Observe Plugin Icon on Tool Bar, very script is working

Expected Behavior

Tampermonkey plugin shows Icon and 1 on it which indicates there 1 active script image This is working

Actual Behavior

Tampermonkey plugin does not show that there any active scripts image This does not work

Specifications

Script

Full script: (Please give an example of the script if applicable.)

But you can do something like this without actual js in it

// ==UserScript==
// @name         Easy Apply -> make it even easier
// @version      0.0.6
// @description  Makes "Easy Apply" actually easy: 1) auto uncheck company to follow 2) closes pop after submit
// @author       kyxap | https://github.com/kyxap
// @match        https://www.linkedin.com/jobs/search/*
// @match        https://www.linkedin.com/jobs/collections/recommended/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=linkedin.com
// @grant        none
// ==/UserScript==

(function () {
    'use strict';
})();
7nik commented 12 months ago

@math? Did you mean @match?

https://www.linkedin.com/jobs/ doesn't match any @match from the script, and if happens soft navigation to any page, the script won't be loaded either. Userscripts get loaded only at hard navigation.

You can try to match https://www.linkedin.com/* and use onurlchange event to run your code on the target pages.

However, in such cases, I check the presence of the target element and ignore url.

kyxap commented 12 months ago

@math? Did you mean @match?

yeah, fixed

https://www.linkedin.com/jobs/ doesn't match any @match from the script, and if happens soft navigation to any page, the script won't be loaded either. Userscripts get loaded only at hard navigation. yeah it does not, the idea is to navigate to one those:

// @match        https://www.linkedin.com/jobs/search/*
// @match        https://www.linkedin.com/jobs/collections/recommended/*

without opening page directly You can try to match https://www.linkedin.com/* and use onurlchange event to run your code on the target pages.

hm seems very tricky, how this will be different from using document.URL and do custom match and if match just execute the script?

7nik commented 12 months ago

But you cannot listen for changes of document.URL.

// @grant        window.onurlchange

function matchScript ({ url }) {
  if (url.startsWith("https://www.linkedin.com/jobs/search/") || url.startsWith("https://www.linkedin.com/jobs/collections/recommended/") {
    runScript();
  }
}

window.addEventListener("urlchange", matchScript);

matchScript({ url: location.href }); // check the initial location

function runScript () {
  // main code
}

Update: of course, you need to take into account that runScript can be run multiple times (again after each navigation).

derjanb commented 12 months ago

Thanks @7nik

Duplicate of https://github.com/Tampermonkey/tampermonkey/issues/1639#issuecomment-1310077826

kyxap commented 12 months ago

it does not work for me, addEventListener calls matchScript only 1 time, when I go for page url change matchScript never triggers

upd: it works you do back, forward via browser buttons but not when you interact with the page

upd2: found another way to overcome that issue