7

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.com/*
// @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.net/documentation.php and I didn't find anything, maybe I reviewed it too quickly?

So, any suggestions there?

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
    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
    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
    Aug 28, 2015 at 16:25

1 Answer 1

15

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
4
  • Thanks, but I have still have my initial issue, and is that for some reason the code doesn't want to execute inside it, or at least part of it. I will explain it, let me edit the post ;)
    – z3nth10n
    Aug 28, 2015 at 19:52
  • Maybe it's good to put the two ways, on for 46 users and the other for 44 users.
    – z3nth10n
    Aug 28, 2015 at 19:53
  • Yes, your answer is right, the problem is mine, I will try to make a quick fix and I will you in a minutes.
    – z3nth10n
    Aug 28, 2015 at 20:00
  • Sorry, delete your last comment I think I solved it, thanks a lot! :D
    – z3nth10n
    Aug 28, 2015 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.