繁体   English   中英

将脚本注入新打开的标签页-Chrome扩展程序

[英]injecting a script into newly opened tab - Chrome Extension

我编写了一个扩展程序,以在单击按钮时运行此代码段,我需要做的是在run()打开新标签后注入另一个已被删除的代码。

我将如何去做呢?

content_script.js:15 Uncaught TypeError: Cannot read property 'onDOMContentLoaded' of undefined
    at injectTheScript (content_script.js:15)
    at run (content_script.js:9)
    at content_script.js:12

请帮忙。

 let n = 0; function run() { var links = document.getElementsByClassName('cur_pointer open_in1 new_act_btn col_in center big_link'); links[n].click(); } run(); function injectTheScript() { chrome.webNavigation.onDOMContentLoaded.addListener(function(details) { const tabId = details.tabId; chrome.tabs.executeScript(tabId, { code: 'document.getElementsByClassName(\\'glyphsSpriteHeart__outline__24__grey_9 Szr5J\\')[0].click();' }); }); } injectTheScript(); 

您只能从后台脚本访问chrome.webNavigation ,而不能从内容脚本访问chrome.webNavigation (可以从content_script.js使用它)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM