【问题标题】:Content Script is not executing in new tab after page load页面加载后,内容脚本未在新选项卡中执行
【发布时间】:2021-06-11 15:22:17
【问题描述】:

创建 chrome 扩展,在新标签中打开链接并尝试单击新打开的标签上的按钮 -

我可以使用扩展打开新标签,但内容脚本没有在新标签上执行。

清单文件

{
     "manifest_version": 2,
     "name": " New Tab Launcher",
     "description": "Create the tab and button click ",
     "version": "1.0",
     "icons": {
          "16": "icon.png",
          "48": "icon.png",
          "128": "icon.png"
     },
     "browser_action": {
          "default_popup": "popup.html"
     },
     "permissions": [
          "activeTab",
          "tabs"
          ],
     "content_scripts": [
          {
               "matches": [
                    "http://*/*", "https://*/*"
               ],
               "js": [
                    "jquery-3.6.0.slim.min.js","contentScript.js"
               ],
               "run_at": "document_end"
          }
     ]
}

popup.js

document.addEventListener('DOMContentLoaded', function() {
    var checkPageButton = document.getElementById('addMeetingUrl');
    checkPageButton.addEventListener('click', function() {
        chrome.tabs.create({'url': "youtube.com"});
    }, false);
  }, false);

contentScript.js

alert("This is test");
$(document).load(function (e) {
     alert("Testing content script" +$('#logo').text());
});

在上面第一个警报成功通过下一个脚本没有启动。

我已尝试删除 $(document).load 并添加 $(document).ready 两者都不适合我。

【问题讨论】:

    标签: javascript google-chrome-extension


    【解决方案1】:

    #logo 可以在加载和就绪事件触发后添加到 DOM。 如果我说的是真的,请检查调试器。 如果是这样,您将需要使用 MutationObserver。

    【讨论】:

      【解决方案2】:

      尝试删除“run at”,因为它将默认为“document_idle”,这通常是更安全的选项

      【讨论】:

        猜你喜欢
        • 2016-02-24
        • 2012-12-04
        • 2017-07-30
        • 2011-09-02
        • 2012-09-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多