sniff1

resolved [WinPC] ViolentMonkey doesn't always run

7 posts in this topic

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');

 

Share this post


Link to post
Share on other sites

Can you provide a link to the script so others can test?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Hmmm it does seem a bit random. Although even on Chrome it did seem to depend on what link you clicked at some times.

Share this post


Link to post
Share on other sites

Posted (edited)

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)

Edited by sniff1

Share this post


Link to post
Share on other sites

Posted (edited)

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 });

 

Edited by bayas
code changed.
2 people like this

Share this post


Link to post
Share on other sites

I realized that after seeing the lazy load part of youtube, was going to write a mutation observer.
You made my life easier.

Awesome, thank you!

1 person likes this

Share this post


Link to post
Share on other sites