2010-09-18 76 views
8

是否可以從地址欄加載遠程JavaScript文件?通過地址欄加載遠程JavaScript文件

我一直試圖把這個到地址欄:

javascript:src='http://depot.com/file.js';funcname(); 

我不使用這個壞事情。我只是測試我的網站,就這些。如果你想保護你的網站,你必須學會​​先攻擊它,對嗎?

+0

到地址欄...你是什麼意思? – 2010-09-18 15:04:48

+1

我想你用這個壞東西......「不要做壞事」:) – Topera 2010-09-18 15:07:00

+2

添加幾個unicrons有什麼不好? :) – 2010-09-19 01:24:01

回答

9

我想你應該能夠做到以下幾點:

javascript:(function() { 
    var newScript = document.createElement('script'); 
    newScript.type = 'text/javascript'; 
    newScript.src = 'http://depot.com/file.js'; 
    document.getElementsByTagName('body')[0].appendChild(newScript); 
})(); 

這裏是一個非常有用的例子(粘貼在地址欄中):

javascript:(function() { 
    var newScript = document.createElement('script'); 
    newScript.type = 'text/javascript'; 
    newScript.src = 'http://cornify.com/js/cornify.js'; 
    document.getElementsByTagName('body')[0].appendChild(newScript); 

    for (var i = 0; i < 5; i++) { 
    newScript = document.createElement('script'); 
    newScript.type = 'text/javascript'; 
    newScript.src = 'http://cornify.com/js/cornify_run.js'; 
    document.getElementsByTagName('body')[0].appendChild(newScript); 
    } 
})(); 

瞧:

Stack Overflow cornified

事實上,這是如何包括cornify.com將遠程腳本放入其小書籤中。


UPDATE:

由於@Ben noted in the other answer,它不是簡單的打電話給你的遠程腳本定義的函數。本提出瞭解決這個問題的一個解決方案,但也有另一個解決方案,即角質化正在使用的解決方案。如果您檢出http://cornify.com/js/cornify_run.js,您會看到該文件中只有一個函數調用。您可以將您的funcname()調用放置在單獨的JavaScript文件中,正如cornify所做的那樣,因爲腳本塊保證按照它們插入的順序執行。然後,你必須包括腳本,如下面的例子:

javascript:(function() { 
    var newScript = document.createElement('script'); 
    newScript.type = 'text/javascript'; 
    newScript.src = 'http://depot.com/file.js'; 
    document.getElementsByTagName('body')[0].appendChild(newScript); 

    newScript = document.createElement('script'); 
    newScript.type = 'text/javascript'; 
    newScript.src = 'http://depot.com/file_run.js'; 
    document.getElementsByTagName('body')[0].appendChild(newScript); 
})(); 

file_run.js簡單地包括對funcname()通話。

+0

非常感謝你:) – kapitanluffy 2010-09-19 01:33:44

+1

+1爲整潔的圖形。 – balupton 2010-09-19 01:52:01

+0

所以file_run.js包含funcname(params)對不對? – kapitanluffy 2010-09-19 03:59:56

2

沒有直接的方法來做到這一點,然而常見的黑客行爲是運行幾行JavaScript,將標籤插入當前頁面,並將其src屬性設置爲要運行的腳本的URL:

javascript:var s=document.createElement("script");s.src="http://depot.com/file.js";s.type="text/javascript";document.getElementsByTagName("body")[0].appendChild(s); 

如果你想運行在遠程文件(在你的問題啦funcname())定義的函數,這是比較麻煩一些。這是因爲通過標籤加載腳本是異步運行的,所以文件很可能還沒有完成加載,立即將元素添加到DOM。我能想到的解決這個得到的唯一方法是你插入的元素,然後您可以在包含源文件的末尾調用之前定義的一些功能:

function doStuff() { 
    // run code that depends on the included JS file 
}; 
// include the external script, as per the snippet above 

你會那麼包括對doStuff通話在包含文件的末尾:

if(doStuff) doStuff(); 

最後的結果看起來是這樣的:

javascript:function doStuff(){funcname()};var s=document.createElement("script");s.src="http://depot.com/file.js";s.type="text/javascript";document.getElementsByTagName("body")[0].appendChild(s); 
+0

+1當調用'funcname()'時提到異步問題...我在我的答案(從cornify中借用)中提出了另一個解決方案。 – 2010-09-18 16:31:51

2

不能直接回答,但仍然有幫助。

這裏是一個腳本在JavaScript文件加載書籤時:

javascript:var%20e=document.createElement('script');e.setAttribute('language','javascript');e.setAttribute('src','http://github.com/balupton/ajaxy-bookmark/raw/master/script.js');document.body.appendChild(e);void(0);