javascript - Google Chrome 扩展 - 获取字符串中的选项卡内容

标签 javascript google-chrome

我正在创建一个 Google Chrome 扩展程序,我需要将页面 HTML 作为字符串导入到我的扩展程序中,以便可以使用它。

我已经尝试过:

document.getElementById('container');

但我得到了一个“null”作为返回。我已确定它是正确的 div id。

我使用的代码有问题还是这个方法根本不起作用?

最佳答案

要将容器 div 作为字符串,您应该使用 innerTextinnerHTML

如下:

var asText = document.getElementById('container').innerText;
var asHTML = document.getElementById('container').innerHTML;

如果您在未使用任何方法的情况下得到空答案,则意味着您的 div 不存在或未加载。

编辑:

不要忘记在manifest.json 文件中指定应该执行javascript 的时间。 您可以使用 run_at 参数来执行此操作。

ma​​nifest.json

{
      "name": "The name of your extension",
      "version": "0.0.1",
      "manifest_version": 2,
      "description": "Your description goes here",
          "homepage_url": "http://example.com/",  
      "content_scripts": [
            {
                  "run_at": "document_end",
                  "all_frames": false,
                  "matches": [
                        "*"
                  ],
                  "js": [
                        "*"
                  ]
            }
      ],
      "web_accessible_resources": [
            "*"
      ],
      "permissions": [
            "storage"
      ]
}

关于javascript - Google Chrome 扩展 - 获取字符串中的选项卡内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28255781/

相关文章:

html - CSS:链接悬停在 Chrome 和 Safari 中不起作用

javascript - 在 gulpfile.js 的 Javascript 中舍入 const 变量列表的更好方法是什么?

javascript - 根据用户选择过滤 JSON 嵌套数组

javascript - HTML - 更改复选框选择的 div 最大宽度属性

javascript - 日语字符在 Google Chrome 中显示为其他符号

google-chrome - WebGL - initGL 未定义

asp.net - IE8 下的慢 javascript

javascript - 你如何让 Video.JS 向前和向后跳 15 秒?

python - selenium.common.exceptions.WebDriverException : Message: unknown error: Chrome failed to start: exited abnormally with ChromeDriver Chrome and Selenium

google-chrome - 有没有办法在 Chrome 或 Firefox 关闭时显示桌面通知?