2012-12-09 17 views
0

我想建立一個Chrome擴展,它檢查當前用戶正在查看的頁面的所有鏈接,並檢查該鏈接的目標,如果它是「http:/ /www.youtube.com「,那麼擴展名將更改爲」https://www.youtube.com「Chrome擴展的問題開發,以改變網頁中的錨點

但我的擴展不工作!

,這裏是我的manifest.json

{ 
    "name": "Youtube Host Changer", 
    "version" : "1.0", 
    "manifest_version" : 2, 
    "description" : "It changes Youtube HTTP host to HTTPS", 
    "browser_action": { 
    "default_icon": "icon.png" 
    }, 
    "content_scripts": [ 
     { 
    "matches": ["file:///*/*","http://*/*","https://*/*"], 
    "js": ["jquery.js", "youtube_host.js"] 
    } 
    ] 
    } 

,這裏是我的youtube_host.js

/* 
* Part of the Youtube Host Change Project. 
* Author : Ahmad Faiyaz 
*/ 
function change_links(){ 
var nodes = document.getElementsByTagName("a"); 
    for(var i = 0; i < nodes.length; i++) { 
     var link= nodes[i].href; 
     link=link.replace("http://www.youtube","https://www.youtube"); 
     nodes[i].href=link; 
}; 
} 

window.onload = change_links(); 

回答

0

它添加

"permissions":["<all_urls>"] 

manifest.json文件後的作品。

最終manifest.json檔案

{ 
    "name": "Youtube Host Changer", 
    "version" : "1.0", 
    "manifest_version" : 2, 
    "description" : "It changes Youtube HTTP host to HTTPS", 
    "browser_action": { 
    "default_icon": "icon.png" 
    }, 
    "content_scripts": [ 
     { 
    "matches": ["file:///*/*","http://*/*","https://*/*"], 
    "js": ["jquery.js","youtube_host.js"] 
    } 
    ], 
    "permissions":["<all_urls>"] 
    } 

讓我知道如果你需要更多的信息。

+0

感謝,現在它適用於此頁面https://dl.dropbox.com/u/34972503/test.html, 但如何使它適用於[鏈接](http://www.udacity.com/查看#Course/ph100/CourseRev/1/Unit/3001/Nugget/5001) 這種頁面?將標籤「a」更改爲「iframe」不起作用.. –

+0

@AhmadFaiyaz:它適用於我,iframe具有'src'而不是'href';你做了這個改變嗎? – Sudarshan

+0

邑我改變了它...新的代碼是: http://pastebin.com/hmRDGMFy –

0

我認爲你需要寫

... 
link = link.replace("http://www.youtube","https://www.youtube"); 
... 

string.replace不會改變原始值,但返回一個新的結果字符串。

+0

我已經更新了代碼,但仍然沒有工作! –