开发者

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开发者_C百科_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');
});


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜