在iframe chrome扩展上运行JS

时间:2017-01-13 12:06:48

标签: javascript json google-chrome-extension

我正在创建一个简单的Chrome扩展程序,以便在弹出窗口中加载我无法访问代码的页面。

但该网站没有API来获取我想要的内容。所以我在iFrame中加载了页面并尝试使用JS去除iFrame中我不想要的内容。

的manifest.json:

{
  "manifest_version": 2,

  "name": "Website Ext",
  "description": "My Description",
  "version": "1.0",

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "permissions": [
    "activeTab",
    "https://ajax.googleapis.com/"
  ]
}

Popup.html

<!doctype html>
<html>
  <head>
    <title>My extension</title>
    <style>
      body {
        font-family: "Segoe UI", "Lucida Grande", Tahoma, sans-serif;
        font-size: 100%;
      }
      #status {
        /* avoid an excessively wide status text */
        white-space: pre;
        text-overflow: ellipsis;
        overflow: hidden;
        width: 600px;
        height; 800px

      }
    </style>
    <script src="popup.js"></script>
  </head>
  <body>
    <div id="status">
        <iframe src="http://domain.com" width="590px" height="795px">
    </div>
  </body>
</html>

Popup.JS:

Element.prototype.remove = function() {
    this.parentElement.removeChild(this);
}
NodeList.prototype.remove = HTMLCollection.prototype.remove = function() {
    for(var i = this.length - 1; i >= 0; i--) {
        if(this[i] && this[i].parentElement) {
            this[i].parentElement.removeChild(this[i]);
        }
    }
}

document.getElementById("headerbody").remove();
document.getElementById("footerbody").remove();
document.getElementById("breadcrumb").remove();
document.getElementById("pagetitle").remove();
document.getElementById("actionbar").remove();

然而,JS似乎被忽略了任何有关如何使其工作的建议将不胜感激。

我已经在Chrome开发人员工具中测试了JS,它也删除了我想要的东西。

0 个答案:

没有答案