Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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');
});
like image 348
Kevin Avatar asked Apr 19 '11 07:04

Kevin


People also ask

Do Chrome extensions work automatically?

You can automatically install (force-install) specific Chrome apps and extensions for users in your organization. Users then see these apps and extensions when using Chrome on managed devices or accounts.

Can Chrome auto refresh a page?

Click on the puzzle piece icon, then on “Easy Auto Refresh”. In the popup window, enter the number of seconds after which you want the page to refresh, then click “Start”. The page will automatically refresh every time the timer you set expires.

Can you store arrays in chrome storage?

You can store arrays directly.


1 Answers

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.

like image 80
Chris McFarland Avatar answered Sep 30 '22 17:09

Chris McFarland