3

Possible Duplicate:
Port error while changing chrome extension from manifest v1 to v2

Looking to make my first Chrome Extension, but I've ran into a pickle when I started to look at using the option page's localStorage with the www.example.com/example.html's localStorage. I've looked around for how to pass messages back and forth between the two, with this page on Message Passing, but seems like I'd need to set up the eventListener, and use SendRequests in background.html which isn't allowed in manifest : 2:

There were warnings when trying to install this extension:
'background_page' requires manifest version of 1 or lower.

edit: you need to make the changes to manifest as seen here, to use `bac

The Message Passing page talks about creating a Port, but it's unclear on where to put the listener. If I put the first half into the content.js script, then the other half into options.js, the listener it only active when the options.html is open.

So my question is: How do I pass settings I've chosen in options.html to be usable in my contentscript.js which manipulates the current webpage?


Here's my entire set up:

background.html

 <!doctype html>
<html>
  <head>
    <title>Background Page</title>
    <script src="jquery.min.js"/>
    <script src='background.js'/>

</script>
  </head>
  <body>
  </body>
</html>

background.js

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    if (request.method == "getLocalStorage")
      sendResponse({data: localStorage[request.key]});
    else
      sendResponse({}); // snub them.
});

contentscript.js

chrome.extension.sendRequest({method: "getLocalStorage", key: "status"}, function(response) {
  console.log(response.data);
});

manifest.js

{
    "manifest_version": 2,

    "name": "Image replacer",
    "version": "1.0",
    "description": "A script to replace all images in the browser with images from any given Imgur gallery",
    "browser_action": {
        "name": "Image replace BA",
        "icons": ["icon.png"],
        "default_icon": "icon.png"
    },

    "background": {
        "page": "background.html"
    },

    "permissions": ["storage"],

    "content_security_policy": "script-src 'self' https://www.imgur.com; object-src 'self'",

    "content_scripts": [{
        "js": ["jquery.min.js", "contentscript.js"],
        "matches": ["http://*/*", "https://*/*"]

    }],
    "options_page": "options.html"
}
Community
  • 1
  • 1
TankorSmash
  • 12,186
  • 6
  • 68
  • 106
  • [Background pages](http://developer.chrome.com/extensions/background_pages.html) **do work** in manifest v2. You have to use the new syntax instead (eg `"background": {"scripts": ["background.js"]}`). – Rob W Sep 07 '12 at 15:34
  • See [Port error while changing chrome extension from manifest v1 to v2](http://stackoverflow.com/questions/11913575/port-error-while-changing-chrome-extension-from-manifest-v1-to-v2). The answer to your question is phrased at the bottom of the accepted answer. Before it, additional relevant information is provided (common pitfalls). – Rob W Sep 07 '12 at 15:34
  • @RobW Alright, I've gotten the manifest updated, the contentscript is now creating a request, and I've got a listener in `background.js`, but still getting an undefined response error – TankorSmash Sep 07 '12 at 15:37
  • I'm not good at debugging invisible code ;) Can you edit your question to provide the relevant code? (the documentation for `onMessage` is partially incorrect by the way, have a look at [this answer](http://stackoverflow.com/questions/11811554/chrome-extension-port-error-could-not-establish-connection-receiving-end-does/11811936#11811936). Does it solve your problem?). – Rob W Sep 07 '12 at 15:40
  • @RobW Edited the code in, you answer looks like it'll work, I'll try it out. – TankorSmash Sep 07 '12 at 15:44
  • You're not using any features of a HTML document. So, I suggest to use `"background": {"scripts": ["jquery.min.js", "background.js"] }` instead. If you still want to use `"background": {"page": "background.html"}`, then you must also use `` - Self-closing script tags don't work. – Rob W Sep 07 '12 at 15:46
  • Thanks for the pointer, I just started getting familiar with HTML/JS last night. Don't I need `background.html` to place the listener there, in order to share the option settings? – TankorSmash Sep 07 '12 at 15:54
  • You're not inserting *any* non-script HTML elements. Therefore, I advise to use a background script (`background.html` will be auto-generated and embed your scripts). The event listeners are bound to the chrome API objects, not to any specific DOM element, so.. – Rob W Sep 07 '12 at 15:59
  • @RobW Great, that solved the issue then. If you care to, please submit this solution as answer and I'll accept it! – TankorSmash Sep 07 '12 at 17:13
  • If you edit the question to phrase your situation, I'll post a matching answer. See, your current Q&A cannot easily be found through Google, because your question's title and introduction doesn't match the actual problem and solution. – Rob W Sep 07 '12 at 21:05
  • @RobW That's fair, as I misunderstood what was actually happening. I'll edit it. We're all friends here. I can see now too, that you properly recognized the question as a duplicate, though I didn't feel the same at the time. – TankorSmash Sep 08 '12 at 01:07

0 Answers0