Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

javascript - chrome extension insert content script on browser action

I am trying to make basically an element highlighter chrome extension. Workflow: - click on browser icon - click on the page - hightlight the element clicked

I am having troubles in running content scripts upon browser action using manifest_version:2 When I inspect the popup that appears it says:

Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' chrome-extension-resource:" (popup.html:5).

Which is where the inline script in popup.html is and the script does not work

I have:

manifest.json:

{
   "browser_action": {
      "default_icon": "images/icon.gif",
      "default_popup": "popup.html"
   },
   "manifest_version": 2,
   "description": "MEH!",
   "name": "My First Extension",
   "permissions": [
      "tabs", "http://*/*", "https://*/*"
   ],
   "version": "0.1"
}

popup.html:

<html>
  <head>
  </head>
  <body>
    <script>
      chrome.tabs.executeScript(null,{
        code:"document.body.style.backgroundColor='red'"
      });
    </script>
    <div id='msg' style="width:300px">...</div>
  </body>
</html>

Any help would be very much appreciated

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Turns out I could not read the error properly until I saw it in here

Apparently manifest v2 does not allow you to have inline scripts, so you just need to

src="path_to_the_file.js"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...