我正在尝试构建一个 chrome 扩展。它获取用户名的背景图片 url 并将其放入页面。但它给出了意想不到的错误。错误文本是:
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)
扩展文件夹包含这些文件:
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> • ");
});
}
背景.html:
<html>
<head>
<script type="text/javascript" src="/javas.js"></script>
</head>
<body>
</body>
</html>
jquery.min.js 取自 http://code.jquery.com/jquery-1.8.2.min.js
list .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 page at https:... run insecure content from http://...”)只是一个警告,并不严重。
第二个错误是执行上下文不同造成的。参见 Chrome extension code vs Content scripts vs Injected scripts .通过查看第一个警告可以看出,发出了 JSONP 请求。这意味着 <script>
元素被注入(inject)到页面中。此脚本调用了一个无法找到的函数 ( jQuery182....
),因为 jQuery 是在内容脚本的上下文中加载的。
要解决这个问题,要么 inject action.js
在页面中,或(推荐)添加 https://api.twitter.com/*
到 list 文件的权限部分,不要使用 ?callback=?
在您传递给 $.getJSON
的 URL 中.
关于javascript - Chrome : Uncaught ReferenceError jQuery is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13099404/