background.js页面内容时,标签在Chrome扩展名改成
var htmlcontent = null;
chrome.tabs.onUpdated.addListener(function() {
onWindowLoad();
alert(htmlcontent);
});
chrome.tabs.onHighlighted.addListener(function(){
onWindowLoad();
alert(htmlcontent);
});
function onWindowLoad() {
chrome.tabs.executeScript(null, {
file: "getPagesSource.js"
}, function() {
if (chrome.extension.lastError) {
htmlcontent = 'There was an error injecting script : \n' + chrome.extension.lastError.message;
}
});
}
chrome.extension.onMessage.addListener(function(request, sender) {
if (request.action == "getSource") {
htmlcontent = request.source;
}
});
getPagesSource.js
function DOMtoString(document_root) {
return document.body.innerText ;
}
chrome.extension.sendMessage({
action: "getSource",
source: DOMtoString(document)
});
我开发一个Chrome扩展。我能够在重新加载当前窗口的正文内容,但是当我更改选项卡时,我无法获取内容。前一个标签的内容正在保存。我使用了错误的API吗?或者当我在Chrome中更改标签时,如何触发onUpdate功能?
“更改标签”我的意思是,切换之前加载的标签。我需要使用哪些chrome API,以便chomre.tabs.executeScript可以在“CHANGED TAB”上执行?为了提供更清晰的视图,假设我在Chrome浏览器中加载了标签1,2和3。目前我在标签1中。当我切换到标签2时,我需要捕捉到内联文本。但在我的代码中,当我切换到标签2时,我得到了标签1的innerText。我如何获取已更改标签的innerText而不是以前标签的文本? – nikhil