Chrome:未捕获ReferenceError未定义jQuery [英] Chrome: Uncaught ReferenceError jQuery is not defined

查看:108
本文介绍了Chrome:未捕获ReferenceError未定义jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个Chrome扩展。它将用户名的背景图片网址放到页面中。但它会带来意想不到的错误错误文本是:



manifest.json:

  {
name:Twitter Background,
version:1.0,
background_page:background.html,
description:将背景图片网址放到个人资料页面。,
content_scripts:[
{
matches:[http://twitter.com/*\",\"https://twitter.com/*],
js :[jquery.min.js,action.js],
all_frames:true

}
]
}


解决方案

第一个错误内容来自http:// ...)仅仅是一个警告,而非关键。

第二个错误是由执行上下文的差异造成的。查看 Chrome扩展代码与内容脚本与注入脚本 。正如您通过查看第一条警告所看到的那样,会生成一个JSONP请求。这意味着在页面中注入< script> 元素。这个脚本调用了一个函数( jQuery182 .... ),这个函数找不到,因为jQuery是在内容脚本的上下文中加载的。



要解决这个问题,要么在页面中注入 action.js ,或者(推荐)添加 https: //api.twitter.com / * 添加到清单文件的权限部分,并且不要在URL中使用?callback =?你传递给 $。getJSON


I am trying to build a chrome extension. It takes the username's background picture url and puts it to the page. But it gives unexpected error. The error text is:

The page at https://twitter.com/jack ran insecure content from http://api.twitter.com/1/users/show.json?screen_name=jack&callback=jQuery18207089675806928426_1351333462593&_=1351333462594.

Uncaught ReferenceError: jQuery18207089675806928426_1351333462593 is not defined (anonymous function)

The extension folder contains these files:

action.js // background.html // jquery.min.js // manifest.json

action.js:

var userPage = document.URL;

var arr = userPage.split("/");
var username = "";
for(i=3;i<4;i++)
    username += arr[i];

var page = "http://api.twitter.com/1/users/show.json?screen_name="+username+"&callback=?"; 

background();

function background() {
        $.getJSON(page, function(data) {
            $("span.screen-name").append(" • <a href='"+data.profile_background_image_url+"'>B</a> • ");
        });     
}

background.html:

<html>

<head>
<script type="text/javascript" src="/javas.js"></script>
</head>

<body>
</body>

</html>

jquery.min.js is taken from http://code.jquery.com/jquery-1.8.2.min.js

manifest.json:

{
  "name": "Twitter Background",
  "version": "1.0",
  "background_page": "background.html",
  "description": "Puts the background image url to the profile page.",
   "content_scripts": [
    {
      "matches": ["http://twitter.com/*","https://twitter.com/*"],
      "js": ["jquery.min.js","action.js"],
      "all_frames":true

    }
  ]
}

解决方案

The first error ("The page at https:... ran insecure content from http://...") is just a warning, and non-critical.

The second error is caused by the difference in execution context. See Chrome extension code vs Content scripts vs Injected scripts. As you can see by looking at the first warning, a JSONP request is made. This means that a <script> element is injected in the page. This script calls a function (jQuery182....), which cannot be found, because jQuery was loaded in the context of a content script.

To solve the problem, either inject action.js in the page, or (recommended) add https://api.twitter.com/* to the permissions section of your manifest file, and don't use ?callback=? in the URL you're passing to $.getJSON.

这篇关于Chrome:未捕获ReferenceError未定义jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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