Can Greasemonkey work with the file:// protocol?

房东的猫 提交于 2019-11-30 03:39:59

问题


I have a simple Greasemonkey script:

// ==UserScript== 
// @name hello
// @namespace http://www.webmonkey.com 
// @description A test of accessing documents using file:// protocol
// @include     http* file*
// @grant       none
// ==/UserScript==

alert("hi");

It works fine as long as the URL is of the form http://... How do I also get the script to run on URLs of the form file://...?

In the User Settings section I have http://* and file://* as the included pages and in the Script Settings section I have http* file* in the "Included Pages" box.


回答1:


See "Greaseable schemes" in the Greasemonkey docs. Greasemonkey ignores the file:// protocol by default.

For scripts to work with file:// paths, you need to open about:config and set extensions.greasemonkey.fileIsGreaseable to true.

You might possibly have to restart Firefox for this setting to take effect.



Also, // @include http* file* is invalid syntax. You would use:

// @include     http://*
// @include     https://*
// @include     file://*

except, avoid using such global includes as much as you can. Tune to the script to just the domain(s) and/or page(s) you explicitly target.

This: avoids unexpected side effects, increases performance, and reduces the chances of being pwned by some "zero day" exploit.


I also recommend that you delete the User Settings options for scripts you write yourself. This will only lead to heartache and confusion later. ;) Use the metadata section of the script, only, for scripts you control.



来源:https://stackoverflow.com/questions/19910568/can-greasemonkey-work-with-the-file-protocol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!