在chrome扩展中,不能在content_script中加载jquery文件 [英] Not working loaded jquery file in the content_script in chrome extension

查看:1876
本文介绍了在chrome扩展中,不能在content_script中加载jquery文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是这个Chrome扩展插件开发的新手。



我的要求是在没有该js文件时将jquery.js文件加载到内容脚本中(For Ex :我正在检查jquery.js文件,fancybox.js文件,如果它没有加载这些文件。)



当我在内容脚本中实现这个逻辑时,它是加载jquery.js文件。之后,它不在内容脚本中工作。它显示$(或)jQuery是未定义的。每次加载时,都不会在内容脚本中执行。



以下是我实现的代码。

  document.getElementById ('someid')。onclick = loadJs(); 

函数loadJS(){
if(tyof jQuery == undefined || tyof jQuery!='function')
{
var jqscript = document.createElement( '脚本');
jqscript.type ='text / javascript';
jqscript.async = true;
//脚本的源
jqscript.src =...... url to jquery.js file ..............;

//检查脚本是否加载
jqscript.onload = function(){
if((!!this.readyState || this.readyState ==loaded || this.readyState ==complete)){
console.log(Script loaded - );
//在这里显示错误
alert(typeof jQuery);

}
};

//获取文档头文件
var head = document.getElementsByTagName('head')[0];
//向脚本添加脚本 - 否则IE会抱怨
head.appendChild(jqscript);


$ b code
$ b

请在此建议。 p>

感谢。

解决方案

Chrome扩展程序无法访问脚本网页加载和使用。



谷歌将这个孤立的世界称为:

http://code.google.com/chrome/extensions/content_scripts.html



您不能...


使用由其扩展页面定义的变量或函数

使用由网页或其他内容脚本定义的变量或函数

因此,它不会重要的是那里有什么脚本。重要的是,你需要在清单中包含你需要的脚本:

 content_scripts:['jquery.js',' myScript.js','optionsScript.js'],

列表的顺序很重要,如果您的脚本使用jQuery,然后jquery.js应该在它之前。


I am new to this chrome extension plugin development. I am developing a new new plugin using this chrome extension.

My requirement is load jquery.js file into the content script when it is not having that js file(For Ex: I am checking for jquery.js file,fancybox.js file and if it is not there load these files. )

When i implement this logic in content script it is loading the jquery.js file. After that it is not working in content script . It is showing $(or) jQuery is undefined. For every time it is loading,but not executing in the content script.

Here is the code what i implemented.

document.getElementById('someid').onclick = loadJs();

function loadJS() {
if(tyof jQuery == undefined || tyof jQuery != 'function' )
  {
var jqscript = document.createElement('script');
jqscript.type = 'text/javascript';
jqscript.async = true;
// Src of the script
jqscript.src = "......url to jquery.js file..............";

// Check whether script is loaded or not
        jqscript.onload=function(){
            if ((!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) {
                    console.log("Script loaded - ");
                                   // It is showing error here
                                        alert(typeof jQuery);

            }
        };

// Get the document header
var head = document.getElementsByTagName('head')[0];
// Add script to header - otherwise IE complains
head.appendChild(jqscript);

}
}

Please suggest on this.

Thanks.

解决方案

Chrome Extensions do not have access to the scripts the web page loads and uses.

Google calls this isolated worlds:

http://code.google.com/chrome/extensions/content_scripts.html

You cannot...

Use variables or functions defined by their extension's pages

Use variables or functions defined by web pages or by other content scripts

So, it doesn't matter what scripts are there. What matters is that you include the scripts you need in your manifest:

"content_scripts": ['jquery.js', 'myScript.js', 'optionsScript.js'],

The order of the list matters so if your script uses jquery then jquery.js should precede it.

这篇关于在chrome扩展中,不能在content_script中加载jquery文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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