2012-10-27 103 views
3

我正在嘗試構建一個Chrome擴展。它將用戶名的背景圖片網址放到頁面中。但它會帶來意想不到的錯誤錯誤文本是:Chrome:Uncaught ReferenceError jQuery沒有定義

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)

error message

擴展文件夾包含以下文件:

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

folder

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:

{ 
    "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 

    } 
    ] 
} 

回答

4

的第一個錯誤(「在HTTPS頁面:... RAN

<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

manifest.json的拍攝來自http:// ...的不安全內容)只是一個警告,並非關鍵。

第二個錯誤是由執行上下文的差異造成的。見Chrome extension code vs Content scripts vs Injected scripts。正如您通過查看第一條警告所看到的那樣,會生成一個JSONP請求。這意味着在頁面中注入了一個<script>元素。這個腳本調用了一個函數(jQuery182....),因爲jQuery是在內容腳本的上下文中加載的,所以無法找到它。

爲了解決這個問題,無論是injectaction.js頁面,或者(推薦)添加https://api.twitter.com/*到您的清單文件的權限部分,在你傳遞給$.getJSON的網址,而不要使用?callback=?