简体   繁体   中英

injecting a script into newly opened tab - Chrome Extension

I've written an extension to run this snippet of code when a button is clicked, what I need to do is to inject another snipped once run() has opened a new tab.

How would I go about doing this?

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

Please help.

 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使用它)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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