0

What i want to do: Endlessly watching videos on Youtube and when is in description of the video word Licence alert me.

What i did: I did exactly what is here written, but works sometimes and it is keep finding a word on previous page (but just on youtube).

//contentscript.js
    chrome.runtime.onMessage.addListener(                //wait for message from background.js
        function(request, sender, sendResponse) { 
            if (request.message == "pageChange"){        //do when page changed 
                                                         //find word (copied)
                var xpathResult = document.evaluate("(//text()[contains(., 'Licence')])[1]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
                var node=xpathResult.singleNodeValue; 
                if (node==null){                         //text not found
                    sendResponse({message:true});
                }else{                                   //text found
                    sendResponse({message: false});
                } 
            }
        }
    );  

//background.js
    chrome.webNavigation.onHistoryStateUpdated.addListener(function(details) {      //wait for page to change (youtube is tricky enemy)
            chrome.tabs.sendMessage(details.tabId, {message: "pageChange"}, function(response) { //send message to contentscript.js and get response
                if (response.message == false){                 
                        alert("I found it!");
                    }else if(response.message == true){         
                        alert("I didnt found it...");
                    }
            });
        
    });

//manifest.json    
    {
        "manifest_version": 2,
    
        "name": "Send help",
        "version": "0.148",
        
        "permissions": [
            "tabs", 
            "webNavigation", 
            "storage",
            "activeTab"
        ],
    
        "content_scripts": [{
        "matches": ["<all_urls>"],
        "js":["contentscript.js"]
        }],
        "background": {
            "scripts": ["background.js"]
        }        
    }

I also got idea to start finding a word when youtube video starts playing, but i didnt manage to do that. Youtube API is probably not suited for this.

  • The URL is changed before the page is updated. Use an alternative method: [example](https://stackoverflow.com/a/34100952). See also [Is there a JavaScript / jQuery DOM change listener?](https://stackoverflow.com/a/39508954) – wOxxOm Apr 15 '21 at 03:47
  • Thank you for response, i tried the "alternative method, but its not working for me", so i am using a function to wait for description element to load Thats solved, Next problem i have that content script wont load when next video is playing just when F5, so i tried chrome.tabs.executeScript, but then i need manifest v3 a totally redo it, which would took me days (yep, im idiot)- So, how to load content script when page changes? I have to use chrome.webNavigation.onHistoryStateUpdated.addListener, but i dont know how to load that stupid content.js – Vojtěch Fíla Apr 15 '21 at 14:56
  • The example I linked explains why the things you observe happen, the solution works but you need to implement all parts of the solution and probably use `yt-navigate-finish` event. – wOxxOm Apr 15 '21 at 15:09
  • I cant make it work, but i got i idea if event yt-navigate-finish even exists and it doesnt... I fount it in document! YEY! – Vojtěch Fíla Apr 15 '21 at 17:34
  • Thanks, I've updated the answer. – wOxxOm Apr 15 '21 at 17:41
  • But im still stuck on the same thing. Script sending results from the previus page... – Vojtěch Fíla Apr 15 '21 at 20:17
  • That's the job for MutationObserver or periodic polling in setInterval/setTimeout. – wOxxOm Apr 16 '21 at 03:53
  • MutationObserver is not working for me, but Mutation Events did, but now i need to handle with "How to find a string in whatever is that output". – Vojtěch Fíla Apr 16 '21 at 20:23
  • Mutation events is a bad solution since it slows down the entire page. – wOxxOm Apr 17 '21 at 02:46
  • Mutation events is a bad solution - approved – Vojtěch Fíla Apr 17 '21 at 02:47

1 Answers1

0

Update 0.1

That helped alot - Mutation Observers didnt work for me, but Mutation Events did.

//content.js
document.addEventListener("yt-navigate-finish",function (event){
  var insertedNodes = [];
  document.addEventListener("DOMNodeInserted", function(e) {
    insertedNodes.push(e.target);
  }, false);
  console.log(insertedNodes);
})

Now i need to find a word in xy-tree with javascript, so i need to figure out new way.

Update 0.2

The new (disgusting) way!

Page reloads until urls of new and previus reload are same. You ask why, right? Its because im not able to find way to check if in DOMNodeInserted is a word and not freeze browser.

//content.js
document.addEventListener("yt-navigate-finish",function (event){
  var newPage = location.href;
  chrome.storage.sync.get(['key'], function(result) {
    if ((newPage) != result.key){
      chrome.storage.sync.set({key: newPage});
      console.log("Reloading page");
    }else{
      console.log("Dont need to be reloaded");
    }
  });
});
function finWord(){
    var source = document.getElementsByTagName('html')[0].innerHTML;
    var found = source.search("Licence");
    if(found>-1){
      return true;
    }else{
      return false;
    }  
}

BUG: When reloading youtube video in playlist it changes index everytime it reloads.

I managed to waste 100+ hours on that with a terrible result. Help is welcome.