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?
            Asked
            
        
        
            Active
            
        
            Viewed 142 times
        
    0
            
            
        - 
                    It depends on what exactly the scripts do. – wOxxOm Sep 08 '21 at 11:47
2 Answers
0
            
            
        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
 
    
    
        Zouhair Dre
        
- 558
- 4
- 14
0
            
            
        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);`
  });
});
```
 
    
    
        Johan Hoeksma
        
- 3,534
- 5
- 28
- 40
