Home > Back-end >  How to run js script only when chrome extension is active
How to run js script only when chrome extension is active

Time:09-17

Is there a way to run the javascript file only when someone clicks on the chrome extension. I know you can run scripts when the extension is clicked but is there a way to also stop the script from running when the extension is inactive?

CodePudding user response:

You can try sending a request to the extension and make your logic based on the response you receive.

Check Check whether user has a Chrome extension installed for more details

CodePudding user response:

Why not always inject the code like:

// inject.js
const runOn = ()=> { ...}


chrome.runtime.onMessage.addListener((response, sendResponse) => {
          runOn()
});

// Then at click something like this

chrome.tabs.sendMessage(tabs[0].id,"your message"); 

```


Or maybe use 

```
chrome.tabs.create({url: "/my-page.html"}).then(() => {
  chrome.tabs.executeScript({
    code: `console.log('location:', window.location.href);`
  });
});

```


  • Related