Jam*_*mer 1 html javascript google-chrome google-chrome-extension
我正在尝试创建一个chrome扩展程序,它会从特定网站上删除一些内容,然后打开一个新选项卡,并对已删除的数据进行处理.
下面是我做的一个测试,看看我怎么做.不幸的是,我似乎无法执行该newtab-script.js文件,因为我收到此错误:
运行tabs.executeScript时未经检查的runtime.lastError:无法访问url"chrome-extension://FAKEIDgfdsgfdsgfdsgdsgfdsgFAKEID/newpage.html"的内容.扩展清单必须请求访问此主机的权限.at Object.callback(chrome-extension://FAKEIDgfdsgfdsgfdsgdsgfdsgFAKEID/background.js:43:25)
var button = document.createElement("button");
button.classList.add("web-scrape");
button.innerHTML = "scrape web";
document.querySelector('.placeIWantToPutButton').appendChild(button);
button.addEventListener('click', scrapeData);
function scrapeData(){
//do website scraping stuff here...
var fakedata = [{test:"data1"},{test:"data2"}];
//send scraped data to background.js
chrome.runtime.sendMessage({setdata: fakedata}, function(tab){
//callback
});
}
Run Code Online (Sandbox Code Playgroud)
var dataTempStorage = [];
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.setdata) {
dataTempStorage = request.setdata;
chrome.tabs.create({
'url': chrome.extension.getURL('newpage.html')
}, function(tab) {
chrome.tabs.executeScript(tab.id, {
file:chrome.extension.getURL("newtab-script.js")});
});
}
if (request == "getdata") {
sendResponse({data: dataTempStorage});
}
});
Run Code Online (Sandbox Code Playgroud)
chrome.runtime.sendMessage("getdata", function (response) {
doStuff(response.data);
});
function doStuff(){
//Do staff on newpage.html with data scraped from original page
}
Run Code Online (Sandbox Code Playgroud)
// page ready to be filled with awesome content!
Run Code Online (Sandbox Code Playgroud)
原因:无法使用chrome-extension://scheme 将内容脚本注入扩展页.
解决方案:由于您可以控制该html页面,因此只需显式引用内容脚本文件即可.
newpage.html:
<script src="newtab-script.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
并且不要使用executeScript.
| 归档时间: |
|
| 查看次数: |
2767 次 |
| 最近记录: |