Code injection using executeScript never call callback in Chrome extension

送分小仙女□ 提交于 2021-02-08 11:47:18

问题


I am trying to create Chrome extension I am creating (intended to automatic fill form on third-party site - I know what you thought, not for spam, no). So when I am trying to inject JS into this page using executeScript it never calls callback function. Here is the code:

function doStepPopup () {
console.log ("Step "+step+" begins");
debugger;
var tab = curTab;
chrome.tabs.executeScript (null, {
    file: "extfiller.js"
}, function () {
    debugger;
    console.log ("Script injected for step "+step);
.........
}
doStepPopup ();

Second debugger function and console.log and all subsequent code never executing. Any thoughts? Thanks in advance! And sorry for my English...


回答1:


chrome.tabs.executeScript by default injects at document_idle so it might not run on some weird pages that for whatever reason remain in "busy" state.

Solution: force an immediate execution with runAt: 'document_start':

chrome.tabs.executeScript (null, {
    file: 'extfiller.js',
    runAt: 'document_start'
}, function(results) {
    console.log(results);
});


来源:https://stackoverflow.com/questions/42509273/code-injection-using-executescript-never-call-callback-in-chrome-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!