简体   繁体   中英

Chrome extension Javascript running only on icon click

I want my Chrome Extension to load Javascript once user visits a website. But currently, the Javascript is executed only when user click the extension icon and till the extension popup is open.

I saw the answer in this Chrome extension to load the script without clicking on icon question.

My manifest.json is:

{
  "manifest_version": 2,

  "name": "Javascript example",
  "description": "Some description.",
  "version": "1.1",

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "permissions": [
    "activeTab",
    "storage"
  ],
  "content_scripts": [
    {
      "matches": ["*://*/*"],
      "js": ["popup.js"]
    }
  ]
}

The popup.js is:

document.addEventListener('DOMContentLoaded', () => {

  alert("Working");
});

popup.html is:

<!doctype html>
<html>
  <head>
    <title>Getting Started Extension's Popup</title>
    <script src="popup.js"></script>
  </head>
  <body>
  </body>
</html>

The alert dialog is shown only when I click the extension icon (ie when popup.html is run), not when a page loads. I want the popup.js file to execute without user needing to click the extension icon. What am I missing?

Your manifest works, but run_at defaults to document_idle which means that DOMContentLoaded has already fired. This can be fixed by specifying run_at to document_start instead.

{
  "manifest_version": 2,

  "name": "Javascript example",
  "description": "Some description.",
  "version": "1.1",

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },

  "permissions": [
    "activeTab",
    "storage"
  ],
  "content_scripts": [
    {
      "matches": ["*://*/*"],
      "run_at": "document_start",
      "js": ["popup.js"]
    }
  ]
}

Source: https://developer.chrome.com/extensions/content_scripts

You need the content script.

Content scripts are JavaScript files that run in the context of web pages. By using the standard Document Object Model (DOM), they can read details of the web pages the browser visits, or make changes to them.

Here are some examples of what content scripts can do:

  • Find unlinked URLs in web pages and convert them into hyperlinks
  • Increase the font size to make text more legible
  • Find and process microformat data in the DOM

If your content script's code should always be injected, register it in the extension manifest using the content_scripts field, as in the following example.

{
  "name": "My extension",
  ...
  "content_scripts": [
    {
      "matches": ["http://www.google.com/*"],
      "css": ["mystyles.css"],
      "js": ["jquery.js", "myscript.js"]
    }
  ],
  ...
}

Refer this link for more information & implementation.

您可以使用在页面上下文中运行的内容脚本,这样您就可以知道用户何时访问特定网站: https : //developer.chrome.com/extensions/content_scripts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM