Chrome注入脚本不一致问题 [英] Chrome inject script inconsistency issue

查看:83
本文介绍了Chrome注入脚本不一致问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的函数我做的是将用户重定向到一个登录页面,然后注入一个js来登录用户。下面的代码运行良好但不一致,我很难调试它,因为流包含整个页面的刷新。



在我的setLogin.js中,我试着用$(function(){})中的alert()进行调试。我发现有时它运行不了。所以我怀疑脚本有时会被注入某个时间,但为什么会这样?

  chrome.tabs.update(null, {
url:'https://example.com/index.php?act=Login'
},function(){

chrome.tabs.onUpdated.addListener(函数(tabId,changeInfo,tab){
if(changeInfo.status =='complete'){
chrome.tabs.executeScript(null,{$ b $ file:jquery.js
},function(){

chrome.tabs.executeScript(null,{
code:'var passedData = {username:''+ username +',pass: (),{$ b $ file:setLogin.js
},function() {
window.close(); //关闭弹出窗口

});
});
});

}
});
});


解决方案

默认脚本是在 document_idle 它不能与jQuery保持一致,可能是因为它很大或使用了一些异步初始化。解决方法:明确指定注入脚本应该立即运行。

  chrome.tabs.executeScript({file:jquery.js,runAt:document_start} ,函数(result){
});


Below function I did was to redirect user to a login page, and then inject a js to login the user. The code below worked well but not consistent, I hardly can debug it because the flow contain refresh of the whole page.

in my setLogin.js I try to debug with alert() wrap within $(function(){}); I found that sometime it run sometime it doesn't. So I suspect the script sometime got injected sometime not, but why is it like so?

chrome.tabs.update(null, {
    url: 'https://example.com/index.php?act=Login'
}, function () {

    chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
        if (changeInfo.status == 'complete') {
            chrome.tabs.executeScript(null, {
                file: "jquery.js"
            }, function () {

                chrome.tabs.executeScript(null, {
                    code: 'var passedData = {username:"' + username + '",pass:"' + pass+'"}'
                }, function () {
                    chrome.tabs.executeScript(null, {
                        file: "setLogin.js"
                    }, function () {
                        window.close(); //close my popup

                    });
                });
            });

        }
    });
});

解决方案

By default scripts are injected at document_idle which doesn't work consistently with jQuery, probably because it's big or uses some asynchronous initialization.

Solution: explicitly specify that the injected scripts should run immediately.

chrome.tabs.executeScript({file: "jquery.js", runAt: "document_start"}, function(result) {
});

这篇关于Chrome注入脚本不一致问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆