-1

I am working on a school project/ Chrome extension that assists the Checkout progress of a Website, i declared in my mainfest.json that a content.js file should run if it matches a certain URL.

"content_scripts": [
         {
            "matches": [
                "https://www.solebox.com/de_DE/checkout?stage=payment*"
            ],
            "js": ["payment.js"]
        },
        {
            "matches": [
                "https://www.solebox.com/de_DE/checkout?stage=placeOrder*"
            ],
            "js": ["placeorder.js"]

as you can see in the link, there are different stages. So from stage "payment" to stage "place order" the page won't refresh, ergo the script won´t notice that the URL changed so the js cant be executed. Is there any way the chrome extension can always check the URL?

  • You might find this question helpful. A good way to manage these cases is to use hash to change the location URL without triggering a reload https://stackoverflow.com/questions/6390341/how-to-detect-if-url-has-changed-after-hash-in-javascript – Luca Corsini Jan 04 '21 at 10:56
  • See also [Is there a JavaScript / jQuery DOM change listener?](https://stackoverflow.com/a/39508954) – wOxxOm Jan 04 '21 at 14:50

1 Answers1

0

You could use try using webNavigation. Just check the tab-url and inject your desired script based on that.

chrome.webNavigation.onBeforeNavigate.addListener(function (tab) {
  chrome.tabs.get(tab.tabId, function (results) {
        if (results.url.match("some URL")) {
            chrome.tabs.executeScript(tabId, {
                file: "someScript.js" 
            });
        } 
    });
});

You can read about the webNavigation API here

Luka Momcilovic
  • 159
  • 2
  • 16
  • okay but i where should i paste it? because the manifest.json file isn't javascript – Elias Mende Jan 04 '21 at 17:20
  • just add a background-script to the manifest file and then add this stuff in the background.js file. Here is a link to an explanation: https://dev.to/michaeldscherr/building-a-simple-chrome-extension-1mal – Luka Momcilovic Jan 04 '21 at 17:50