Lid |
|
Ok, het is gelukt:
Popup:
<html>
<head>
<script>
function makeRequest(act){
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendRequest(tab.id, {action: act, tabid: tab.id}, function(response) { });
});
}
</script>
</head>
<body>
<input type="button" id="alert" value="Alert" onClick="makeRequest(this.id);" />
<input type="button" id="addbutton" value="Add Button" onClick="makeRequest(this.id);" />
</body>
</html>
function makeRequest(act){ chrome.tabs.getSelected(null, function(tab) { chrome.tabs.sendRequest(tab.id, {action: act, tabid: tab.id}, function(response) { }); }); } <input type="button" id="alert" value="Alert" 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["alert"] = "alert(test);";// Test staat al in pagina
codes["addbutton"] = "var a = document.createElement('button');document.documentElement.appendChild(a);";
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
switch(request.action){
case "alert":
injectCode(codes["alert"]);
break;
case "addvisibilities":
injectCode(codes["addbutton"]);
default:
break;
}
});
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["alert"] = "alert(test);";// Test staat al in pagina codes["addbutton"] = "var a = document.createElement('button');document.documentElement.appendChild(a);"; chrome.extension.onRequest.addListener(function(request, sender, sendResponse) { switch(request.action){ case "alert": injectCode(codes["alert"]); break; case "addvisibilities": injectCode(codes["addbutton"]); default: break; } });
Lolll |