最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Recall Tampermonkey script when page location changes - Stack Overflow

programmeradmin3浏览0评论

Well, I want to know if its possible to recall a Tampermonkey script when a user changes his location (but the match is still active). For example, my scripts hooks youtube website.

I need to make that the script recalls itself when I change the video, my actual script is:

// ==UserScript==
// @name        xxx
// @namespace    xxx
// @version      1.0
// @description  xxx
// @author       Ikillnukes
// @match        /*
// @match        /*
// @grant        none
// ==/UserScript==

console.log("Tampermonkey hook!");
var script = document.createElement('script');
script.src = document.location.protocol+"//xxx";
(document.body || document.head || document.documentElement).appendChild(script);

As you can see I call console.log for debug it, and it gets called when I refresh or I load the webpage for the first time. But one time I change the video it doesn't get called anymore, and that is what I want to avoid.

I also reviewed this: .php and I didn't find anything, maybe I reviewed it too quickly?

So, any suggestions there?

Well, I want to know if its possible to recall a Tampermonkey script when a user changes his location (but the match is still active). For example, my scripts hooks youtube website.

I need to make that the script recalls itself when I change the video, my actual script is:

// ==UserScript==
// @name        xxx
// @namespace    xxx
// @version      1.0
// @description  xxx
// @author       Ikillnukes
// @match        https://www.youtube./*
// @match        https://youtu.be/*
// @grant        none
// ==/UserScript==

console.log("Tampermonkey hook!");
var script = document.createElement('script');
script.src = document.location.protocol+"//xxx";
(document.body || document.head || document.documentElement).appendChild(script);

As you can see I call console.log for debug it, and it gets called when I refresh or I load the webpage for the first time. But one time I change the video it doesn't get called anymore, and that is what I want to avoid.

I also reviewed this: http://tampermonkey/documentation.php and I didn't find anything, maybe I reviewed it too quickly?

So, any suggestions there?

Share Improve this question asked Aug 28, 2015 at 16:09 z3nth10nz3nth10n 2,4612 gold badges29 silver badges60 bronze badges 3
  • 1 $(document).ready(function(){ .... - then on each page load it will do what you want.. otherwise it does it once.. somewhere in the middle of loading the first time... – Piotr Kula Commented Aug 28, 2015 at 16:11
  • I tried this, and the first thing I that I want to avoid using JQuery, and the second thing is that it doesn't do what I requested, and it's even worse because it happens the same that before, I tried using document.readystatechange and it only make the script not to run. – z3nth10n Commented Aug 28, 2015 at 16:19
  • 1 Right... OK- Well you have to realise Tampermonkey runs in its own context.. like a seperate webpage that runs always. So you need to hook into the page event that the script is attached too. It can be navigate event, like onbeforeunload, or onlaod, or something generic. readystatechange is good start, look in jQuery source how they do it properly.. Basically on each page load, event onready gets fired and it triggers tampermonkey, if you used that. Otherwise your script runs over once per tab and doesnt hook into anything else. – Piotr Kula Commented Aug 28, 2015 at 16:25
Add a ment  | 

1 Answer 1

Reset to default 16

Listen to the custom events used by the youtube script:

window.addEventListener("yt-navigate-start", e => { console.log(e.type); });
window.addEventListener("yt-navigate-finish", e => { console.log(e.type); });

To see all these events in Chrome:

  • use DevTools → Elements panel → Event Listeners
  • use DevTools → Sources panel → Global Listeners
发布评论

评论列表(0)

  1. 暂无评论