[WinPC] ViolentMonkey doesn't always run


Recommended Posts

I have written a script to hide the video window on youtube, and it works when the page is opened directly, or from the address bar.
But if I click any link on youtube, the script doesn't even start.
even the first line of the scripts never runs, which is simply this:

console.log('Starting script');

 

Link to comment
Share on other sites

Like I said, it happens on any script even if you just use it like this:

// ==UserScript==
// @name      Youtube
// @namespace Violentmonkey Scripts
// @include   http://*.youtube.*
// @include   https://*.youtube.*
// @grant     none
// @run-at    document-end
// ==/UserScript==

console.log('start');

You won't see that in the console after clicking a link.
It seems that when you click a link on YouTube the page doesn't actually reload.

Link to comment
Share on other sites

I think it's because Youtube does some Lazy Load for some of the pages...
Any ideas on how to figure it out? I attached a function to the <video> tag, but it doesnt work coming from the main page (which doesn't have a <video> tag)

Link to comment
Share on other sites

On 07.07.2017 at 2:01 PM, sniff1 said:

I think it's because Youtube does some Lazy Load for some of the pages...
Any ideas on how to figure it out? I attached a function to the <video> tag, but it doesnt work coming from the main page (which doesn't have a <video> tag)

Your issue is not related to the ViolentMonkey. You must observe some elements in javascript to change some functionalities. Here is an example for your need.

// ==UserScript==
// @name      Youtube
// @namespace Violentmonkey Scripts
// @include   http://*.youtube.*
// @include   https://*.youtube.*
// @grant     none
// @run-at    document-end
// ==/UserScript==

console.log('My Script Injected!');

function myVideoFunction() {
  console.log('New Youtube video loading..');
  
  var video = document.querySelector('#player video');
  video.playbackRate = 10.0;
}

var observer = new WebKitMutationObserver(function (mutations) {
  mutations.forEach(function (mutation) {
    for (i = 0; i < mutation.addedNodes.length; i++) {
      if (mutation.addedNodes[i].id === 'watch7-main-container') {
        myVideoFunction();
      }
    }
  });
});

var ytPage = document.getElementById('page');
observer.observe(ytPage, { childList: true, subtree: true });

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.