The relationship between ContentScript.js and the Chrome extension

I just want to send the current tab url to my extension:

Here is my manifest. json

{ "name": "DocUrlExtention", "version": "1.0", "manifest_version": 2, "description": "The first extension that I made.", "browser_action": { "default_icon": "icon.png", "default_popup": "popup.html" }, "content_scripts": [ { "matches": ["http://*/*"], "js": ["contentscript.js"] } ]} 

Below is my text contents1.js

 chrome.extension.sendRequest({url: window.location.href}, function(response) { console.log(response.farewell); }); 

Below is my popup.html

 <!doctype html> <html> <head> <title>Getting Started Extension Popup</title> <script> chrome.extension.onRequest.addListener( function(request, sender, sendResponse) { console.log(sender.tab ? "from a content script:" + sender.tab.url : "from the extension"); }); </script> <!-- JavaScript and HTML must be in separate files for security. --> <!--<script src="popup.js"></script>--> </head> <body> <div id="mydiv">Doc Id:</div> </body> </html> 

I don’t see anything in the console. I am new to Chrome extensions.

0
google-chrome google-chrome-extension
May 2, '12 at 15:13
source share
1 answer

The manifest file contains "manifest_version": 2, , which allows the Content Security Policy . By default, Embedded JavaScript will not execute . And it doesn’t allow you to relax in CSP to enable embedded JavaScript .

You have two options:

  • Remove "manifest_version": 2 (which disabled CSP by default).
  • Move embedded JavaScript to an external file.

The second method is recommended as well as suggested in your code ...

 ...
 <! - JavaScript and HTML must be in separate files for security.  ->
 <! - <script src = "popup.js"> </script> ->
 </head>
 ...

PS. The Dev tools for the pop-up window can be opened with the right mouse button on the browser action icon and select the last parameter "Inspect popup" .

+1
May 02 '12 at 16:20
source share



All Articles