【问题标题】:Source code of entire page including frames in chrome extensions整个页面的源代码,包括 chrome 扩展中的框架
【发布时间】:2015-10-17 10:58:12
【问题描述】:

问题是下面给出的链接的延续。

Getting the source HTML of the current page from chrome extension

上述网址中给出的最佳答案获取当前选项卡的源代码。但这并没有得到源代码的 iframe 里面的源代码。

如何访问整个页面的源代码,包括 chrome 扩展中的框架?

【问题讨论】:

  • 欢迎来到 Stack Overflow。我们很乐意为您提供帮助。为了提高您获得答案的机会,以下是一些提示:How do I ask a good question?

标签: javascript google-chrome-extension


【解决方案1】:

使用chrome.tabs.executeScriptallFrames: true 参数将代码注入所有帧。回调函数将接收一个包含所有帧结果的数组:

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) + "...");
        });
    }
});

【讨论】:

  • 工作就像一个魅力。谢谢。为了避免混淆,在 executeScript 函数中添加可选的 IntegerId 参数
  • 使用 chrome.* API 函数,无需为 可选 参数指定 nullundefined。这是一件非常好的事情。
  • 有没有办法从数组中获取帧的url?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多