该问题是下面给出的链接的延续。
Getting the source HTML of the current page from chrome extension
上面 URL 中给出的最上面的答案获取当前选项卡的源代码。但这并不能获取源代码的iframe内的源代码。
如何访问整个页面的源代码,包括 Chrome 扩展中的框架?
最佳答案
使用 chrome.tabs.executeScript 的 allFrames: true
参数将代码注入(inject)到所有框架中。回调函数将接收一个包含所有帧结果的数组:
popup.js:
chrome.tabs.executeScript({
allFrames: true,
code: 'JSON.stringify({ \
url: location.href, \
html: document.documentElement.innerHTML \
})'
}, function(results) {
if (chrome.runtime.lastError || !results || !results.length) {
console.log("Some error occurred", chrome.runtime.lastError);
} else {
results.forEach(function(str, index) {
var info = JSON.parse(str);
console.log("Frame #%d | %s | %s",
index, info.url, info.html.substr(0, 200) + "...");
});
}
});
关于javascript - 整个页面的源代码,包括 Chrome 扩展中的框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33185794/