I defined jQuery.js in manifest.json under content_scripts
"background_page": "html/bg.html",
"content_scripts": [
{
"matches": ["\u003Call_urls\u003E"],
"js": ["js/jquery.js"/]
}
]
and in bg.html, I added a click event handler to find <p>…</p> nodes
<script>
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(null, {code: "alert($('p').text());"});
});
</script>
It works fine if just doing this way. But if I add a js reference in the bg.htm, jQuery then not working anymore, even src=””
<script type="text/javascript" src="../js/jquery.js">
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(null, {code: "alert($('p').text());"});
});
</script>
The background_page and the content_script should be in different scopes, still not find what’s going on here.
If your script tag has a
srcattribute, the contents are not parsed.See here: http://jsfiddle.net/cnK7s/
Instead, use 2 separate
scripttags: