謝謝,它的工作,現在,這裏是我的代碼:
彈出窗口:
function makeRequest(act){
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendRequest(tab.id, {action: act, tabid: tab.id}, function(response) { });
});}
//inputs:
<input type="button" id="addDiv" value="Add Div" onClick="makeRequest(this.id);" />
<input type="button" id="addButton" value="Add button" onClick="makeRequest(this.id);" />
contentScript:
function injectCode(text) {
var script = document.createElement("script");
var parent = document.documentElement;
script.text = text;
script.setAttribute("id", "codeInjection");
script.setAttribute("extension", "Chrome");
parent.appendChild(script);
parent.removeChild(script);
}
var codes = Array();
codes["addDiv"] = "alert('Add a div');";
codes["addButton"] = "alert('Add a button');";
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
switch (request.action) {
case "addDiv":
injectCode(codes["addDiv"]);
break;
case "addButton":
injectCode(codes["addButton"]);
}
});
http://stackoverflow.com/questions/8363639/using- a-pages-functions-in-chrome-extension – c69 2012-01-29 15:23:58