开发者

Calling CONTENT_SCRIPT JS From BROWSER_ACTION Popup HTML

I'm working on a Google Chrome Extension and would like to get an HTML from within my popup HTML to call functions within my loaded javascript file. My manifest follows:

{
    "name": "Extension",
    "version": "1.0",
    "description": "Extension",
    "browser_action": {
        "default_icon": "icon.png",
        "default_title": "Ext",
        "popup": "popup.html"
    },
    "content_scripts": [{
        "matches": ["http://*/*"],
        "开发者_开发问答css": ["ext.css"],
        "js": ["jquery.js","scripts.js"]
    }],
    "permissions": [
        "http://*/*"
    ]
}

As you can see I'm loading in a local copy of jQuery, along with another javascript file for my own personal logic. My popup document looks like this:

<select id="values">
    <option>Foo</option>
    <option>Bar</option>
</select>

And the contents of my scripts.js file follow:

$(function(){
  $("#values").change(function(){
    alert("Foo");
  });
});

This isn't doing what I expect though - alerting "Foo" anytime I change a value in my popup HTML. How can I get these two files to communicate with eachother? Or can they at all?


The content script runs in the context of the loaded page, so that's where it will look for #values. You can't communicate directly between content scripts and the rest of the extension. You'll have to use the messaging API (ready-to-use example on that page).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜