1
我能夠在本地HTML文件來執行以下的Greasemonkey:如何跨協議/計劃執行JavaScript文件?
// ==UserScript==
// @name test
// @include file:///C:/fx/test/a.html
// @grant none
// ==/UserScript==
var scriptElement = document.createElement("script");
scriptElement.type = "text/javascript";
scriptElement.src = "file://c:/fx/alert.js";
document.head.appendChild(scriptElement);
我能夠執行在localhost上的以下內容:
// ==UserScript==
// @name testWeb
// @include http://localhost/test/a.html
// @grant none
// ==/UserScript==
var scriptElement = document.createElement("script");
scriptElement.type = "text/javascript";
scriptElement.src = "http://localhost/test/alert.js";
document.head.appendChild(scriptElement);
不過,我是不是能夠執行以下。 Web服務器中有HTML文件,本地驅動器中有腳本文件。
// ==UserScript==
// @name testWeb
// @include http://localhost/test/a.html
// @grant none
// ==/UserScript==
var scriptElement = document.createElement("script");
scriptElement.type = "text/javascript";
scriptElement.src = "file://c:/fx/alert.js";
document.head.appendChild(scriptElement);
greasemonkey.fileIsGreaseable
在about:config中設置爲true。
我該如何在腳本標記中執行本地腳本文件?
如果您向最後一個樣本添加了// @include file:/// C:/ fx/test/a.html',該怎麼辦? – LGSon
您正試圖跨域訪問文件系統。它在大多數瀏覽器中都是不允許的。 – Bindrid
'fileIsGreaseable'只是意味着即使您在瀏覽器中訪問'file://'url(通常它只攔截「http://」和「https://」),greasemonkey也會運行腳本。 – melpomene