我有一个长篇大论,应该操纵目前开放页(表格)的内容。
To take user input I use popup.html with a few input fields. Input from this fields is transmitted to the page (content script) which executes the desired transformation.
The problem is that I cannot run a function which I define in a content script. I tried to define function in a content_script.js itself and tried to insert it into the page via
var new_script2 = document.createElement( script );
code = "function definition goes here";
new_script2.text = code;
document.getElementsByTagName( head )[0].appendChild(new_script2);
无所作为。 加上我,我也看不到 j的物体,即使我按照建议(http://blog.michael-forster.de/2009/08/using-jquery-to-build-google-chrome.html)“rel=“nofollow” 。
在活动之后如何在 Chrome延伸部分开一个公开的禁忌上运行习俗?
[序言]
Below is what I have now and I solved my problem, the key is to be careful with message passing and functions initialization.
My manifest looks as follows
{
"background_page": "background.html",
"name": "some",
"version": "0.1",
"description": "some",
"browser_action": {
"default_icon": "icon.png",
"default_title": "some",
"popup": "popup.html"
},
"content_scripts": [ {
"js": [ "javascripts/jquery-1.5.min.js", "javascripts/jquerylatest.js", "javascripts/content_script.js" ],
"matches": [ "http://*/*", "https://*/*", "file://*/*" ],
"run_at": "document_end"
} ],
"permissions": [ "tabs", "http://*/", "https://*/" ]
}
In popup.html 我创建了一个县,并指派了一个活动手向背景页发送信息。 html
$( "#create-user" )
.button()
.click(function() {
//~ alert(allFields.val());
//~ var annot = [attrValue, attrType];
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendRequest(tab.id, {"attrValue" : attrValue.val(), "attrType" : attrType.val()},
function readResponse() { console.log("got response in popup");} );
window.close();
});
});
在背景资料中,我听了一名群众发出的信息。
var tab_id = -1;
chrome.tabs.getSelected(null, function(tab) {
tab_id = tab.id;
});
chrome.extension.onRequest.addListener(function(request, sender) {
console.log("in bg.js addlistener method " + request);
chrome.tabs.executeScript(tab_id, {file:"javascripts/content_script.js"});
});
and redirects them further to the content script to use for page modification. content_script listens as follows:
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
//do your manipulation here
});
最后,不要忘记,将jquery图书馆纳入贵展期的javascripts/小册。