是否可以使用Greasemonkey腳本將JS腳本從其他網站添加到頁面以便它們運行?使用Greasemonkey腳本將JavaScript從其他網站添加到頁面
回答
您可以簡單地創建一個腳本元素,並將其添加到文檔
// ==UserScript==
// @name My Fancy New Userscript
// @description enter something useful
// @match http://*/*
// ==/UserScript==
(function() {
var scriptElement = document.createElement("script");
scriptElement.type = "text/javascript";
scriptElement.src = "url to your script";
document.body.appendChild(scriptElement);
})();
如果你只是希望腳本運行,那麼這個足夠。 如果你想在你的腳本中使用jQuery庫,它會變得棘手。 有2種方式,我所知道的:
- 一種是使用的Greasemonkey的require標籤。
- 另一個需要像上面顯示的腳本元素的創建,但您需要等待它加載,所以需要
scriptElement.onload = function() {}
,然後您必須使用unsafeWindow
來訪問您的庫中的變量。
如果這是純粹的greasemonkey腳本,我推薦第一種方法,因爲只有腳本封裝在網站上。
用於'@ require'的+1。您不需要將這些代碼封裝在匿名函數中。 –
我可以多次重複使用變量'scriptElement',以獲取多個外部JavaScript? (如script1.js和script2.js) –
此外,有沒有辦法判斷這是否實際執行並將其添加到網站?因爲它爲我提供了一個腳本,它將從遠程.js腳本中提取實時信息和數據,而不是,因此我的問題。 –
是的,比如,你可以這樣做:
let script = document.createElement('script');
script.src = "http://example.com/somescript.js";
document.body.appendChild(script); // execute the script
document.body.removeChild(script);
它不工作,如果我需要從http加載腳本爲https – fdrv
這將始終執行腳本?在取消孩子之前,不需要等待執行確認? –
- 1. Greasemonkey腳本,從其他頁面腳本調用函數
- 2. 在ChildAction中使用@section將其他腳本添加到頁面
- 3. Greasemonkey腳本阻止網站的其他腳本
- 4. 將計算器從其他網站添加到我的網站
- 5. 使用Greasemonkey腳本將網頁添加到網頁的底部(並設置其大小)?
- 6. 將移動頁面添加到網站
- 7. 從其他網站剪下圖像並將其添加到我的網站
- 8. 我將如何使用JavaScript從其他網站檢索文本?
- 9. 使用Greasemonkey腳本來安裝其他用戶腳本
- 10. 使用腳本將內容發送到其他頁面
- 11. 是否有可能從其他網站拉文本並將其放置在使用javascript的其他網站中?
- 12. Greasemonkey腳本,刷新頁面
- 13. 將腳本添加到ASPX頁面
- 14. 將腳本添加到Sparkview頁面
- 15. 使用greasemonkey將圖像添加到任何網頁?
- 16. div其他網站頁面將其添加到我的網站中的div標記或iframe標記
- 17. 使用greasemonkey將複選框和標籤添加到頁面中
- 18. 使用Greasemonkey將可拖動的窗口添加到頁面
- 19. 使用php腳本從其他網站讀取文件
- 20. 在其他頁面上使用Javascript腳本
- 21. 使用Greasemonkey將html內容添加到頁面的基本方法?
- 22. 使用javascript將腳本加載到頁面
- 23. 使用ECMA腳本將新屬性添加到節點(頁面)
- 24. JavaScript:轉到其他頁面
- 25. 的Magento:將腳本添加到所有頁腳的頁面
- 26. 腳本代碼添加到Twitter網站
- 27. 的Greasemonkey腳本在每個網站
- 28. jquery:從其他頁面導航時,將頁面添加到文檔功能
- 29. 將頁腳添加到HTML頁面
- 30. 將頁腳添加到HTML頁面
可能的重複[包括通過直接代碼注入與greasemonkey遠程JS文件](http://stackoverflow.com/questions/9597770/including-remote-js-file-via-direct-code-injection-with-greasemonkey ) –