小编Mal*_*etz的帖子

从Safari扩展注入jQuery到网页

我只是想从一个safari扩展中将jQuery注入一个网页.但仅限于某些页面,因为将jQuery添加为start-/endscript会将其注入所有页面,这会使浏览速度变慢.我通过使用onload函数创建脚本标记来尝试它:

var node = document.createElement('script');    
node.onload = function(){
    initjquerycheck(function($) {
        dosomethingusingjQuery($);
    });
};
node.async = "async";
node.type = "text/javascript";
node.src = "https://code.jquery.com/jquery-2.0.3.min.js";
document.getElementsByTagName('head')[0].appendChild(node);
Run Code Online (Sandbox Code Playgroud)

检查是否加载了jquery我使用:

initjquerycheck: function(callback) {
    if(typeof(jQuery) != 'undefined'){
        callback(jQuery);
    }else {
        window.setTimeout(function() { initjquerycheck(callback); }, 100);
    }
}
Run Code Online (Sandbox Code Playgroud)

但是typeof(jQuery)仍未定义.(使用console.log()检查).只有从调试控制台调用console.log(typeof(jQuery)),它才会返回'function'.任何想法如何解决?提前致谢!

javascript safari jquery safari-extension

2
推荐指数
1
解决办法
2516
查看次数

标签 统计

javascript ×1

jquery ×1

safari ×1

safari-extension ×1