通过将null作为窗口ID传递给getSelected,您可以获得弹出窗口的选定选项卡。 ()在弹出式窗口,你可以听延期事件和执行脚本的内容推送到弹出:
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
if (request.action == "content")
{
console.log('content is ' + request.content.length + ' bytes');
}
});
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.executeScript(tab.id, { file: 'scripts/SendContent.js' });
});
最后内容脚本...我有它作为“脚本/ SendContent.js”我的扩展文件夹,但剧本是很简单的,你可以通过将代码中的代码属性而不是名称的对象的文件属性执行它传递给executeScript:
console.log('SendContent.js');
chrome.extension.sendRequest({
action: "content",
host: document.location.hostname,
content: document.body.innerHTML
}, function(response) { }
);
结果:
POPUP: content is 67533 bytes
如果遇到问题,请使用console.log()并右键单击您的页面或浏览器操作来检查它并在控制台上阅读您的消息(或从此处调试脚本)。