1

I am trying to inject a content script into the results page of YouTube (after typing something in the search box on YouTube home page and clicking search). The script is not injecting.

I did some research and found that a possible reason is that the URL is being changed by history.push(). But after playing around with the methods, I am still not getting it to work. Every time, I have to refresh the results page for the script to inject.

Do any of you know what is going on?

Here is my code.

Manifest.json

"content_scripts": [

{
  "css": ["results.css"],
  "matches": ["http://www.youtube.com/results*"],
  "js": ["content.js"]
},

{
  "css": ["home.css"],
  "matches": ["https://www.youtube.com/"],
  "js": ["content.js"]
}

content.js:

history.replaceState(stateObj, '', "resultsSearched");
window.onpopstate = function (event) {
  console.log("printed?");
  chrome.extension.sendMessage("Rerun script");
};

background.js

chrome.extension.onMessage.addListener(function(message, sender, callback) {
  if (message == "Rerun script") {
    chrome.tabs.executeScript(
      sender.tab.id, {
      file: "results.js"
    }, function() {
      console.log("completed");
    });
  }
});
colinified101
  • 39
  • 1
  • 3
  • See [How to detect page navigation on YouTube and modify its appearance seamlessly?](https://stackoverflow.com/a/34100952). Don't forget to specify the entire site in `matches`. – wOxxOm Dec 12 '20 at 20:44

0 Answers0