2013-07-01 62 views
0

所以我正在爲我自己的個人使用製作一個Safari擴展程序,它根本不工作。 我試圖跳過adf.ly並直接進入網站。但它根本沒有做任何事情。 我試過提醒當前的URL和應該的新網址,它們甚至都沒有顯示。試圖建立safari擴展。不工作-.-

Global.html

<!DOCTYPE html> 
<script type='application/javascript'> 
// Skip Adf.ly 
// Made by Austen Patterson 
// For Safari 
//(C) Copyright 2013 Austen Patterson. 

safari.application.addEventListener("start", performCommand, true); 
safari.application.addEventListener("validate", validateCommand, true); 


// Function to perform when event is received 
function performCommand(event) { 
    // Make sure event comes from the button 
    if (event.command == "skip") { 
    var url = this.activeBrowserWindow.activeTab.url; 
    var newurl = url.replace(/http:\/\/adf\.ly\/(\d+)\//, ''); 
    location.href(newurl); 
    window.open(newurl,"_self"); 
    return; 

    } 
} 

</script> 

,這裏是我的分機建設者設置。 enter image description here

回答

0

您不能在全局頁面中使用alert。但是,您可以使用console.debug,這樣可以提供幫助。

但是,更大的問題是,您將全局頁面視爲可以訪問您要通過window對象修改的頁面。它不這樣工作。 location.href不指向任何東西,window.open可能無法在全局範圍內工作。 (這些都是滲漏的全局,這是你想要避免的事情。)我沒有測試過,但這樣的事情應該工作:

safari.application.addEventListener("beforeNavigate", function adflyChecker(event) { 
    var url = event.url.replace(/http:\/\/adf\.ly\/(\d+)\//, ''); 
    safari.application.activeBrowserWindow.activeTab.url = url; 
}, true); 

這對導航工作的優勢,而不是需要你手動點擊按鈕。如果您將您的擴展程序配置爲僅適用於adf.ly網址,那麼您可以確定只有在適當的時候纔會觸發您的代碼。

有關Safari擴展的體系結構的更多信息,請訪問in the docs