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.