Chrome扩展程序:加载隐藏页面(不含iframe)

Chr*_*oot 6 google-chrome google-chrome-extension

有没有办法加载一个隐藏在用户之外的页面?

我无法iframe在后台页面中使用,因为该页面具有帧破坏技术.

我不能使用XHR,因为页面有AJAX - 我需要它(动态生成的)DOM.

Rob*_*b W 10

我担心除了插入iframe之外你没有任何其他选择.要破坏iframe破坏者,您可以采用以下技术:

  • 如果iframe被iframe阻止X-Frames-Option: DENY,只需使用webRequestAPI 删除标题- 请参阅Chrome扩展程序中的绕过X-Frame-Options DENY?.
  • 如果帧破坏者使用类似的东西

    if (top !== self) {
        top.location.href = location.href;
    }
    
    Run Code Online (Sandbox Code Playgroud)

    然后通过sandbox在iframe上设置属性来阻止脚本导航:

    var frame = document.createElement('iframe');
    frame.sandbox = 'allow-scripts';
    frame.src = 'data:text/html,<script>' +
        'if (top !== self) { top.location.href = location.href;}' +
        'alert(" (runs the rest of the code) ");' + 
        '</script>';
    document.body.appendChild(frame);
    
    Run Code Online (Sandbox Code Playgroud)

    导航将被阻止而不会丢失任何错误.以下消息记录到控制台:

    不安全的JavaScript尝试从带有URL'(.... URL of frame ..)'的URL启动带有URL'(...首​​页的URL ...)的帧的导航.尝试导航顶层窗口的框架是沙箱,但未设置"允许顶部导航"标记.

这些方法总是有效,除非:

  • 该页面包含<meta http-equiv="X-Frame-Options" content="deny">.
  • 帧破坏脚本实现为 if (top === self) { /* run code*/ }

在这些情况下,除了打开新选项卡,阅读其内容,然后关闭它之外别无选择.见chrome.tabs.createchrome.tabs.remove.