Kevin
Kevin

Reputation: 635

Chrome Extension auto triggering

I am making a chrome extension to find all links in a page I am using a browser_action. I am getting a bug on when you refresh the page, the chrome extension automatically triggers the javascript code. How would I make it so that if you hit the refresh on the browser the extension doesn't trigger? I only want it to work when the click the little icon on the toolbar.

This is how my manifest looks like

{
  "name": "Find all first",
  "version": "1.0",
  "description": "Find all Linkes",
  "browser_action": {
    "default_icon": "icon.png",
    "popup": "popup.html"
  },
  "icons": {
    "16": "icon.png",
    "128": "icon-big.png"
  },
  "permissions": [
    "tabs",
    "http://*/*",
    "https://*/*"
  ],
  "content_scripts": [ {
    "matches": ["http://*/*", "https://*/*"], 
    "js": ["content.js"]
  }]
}

I am then calling this in my popup.html

chrome.tabs.executeScript(null, {file: 'content.js'}, function() {
  console.log('Success');
});

Upvotes: 5

Views: 5861

Answers (1)

Chris McFarland
Chris McFarland

Reputation: 6169

Because you have contents_scripts defined in your manifest, content.js is being run as a content script every time a page is loaded that matches your matches (so, any webpage really).

To only run content.js on the page when the user clicks your Page Action button, remove the content_scripts section from your manifest, so that no scripts are run automatically. Then, when the Page Action button is clicked, popup.html will execute content.js as it should.

Upvotes: 7

Related Questions