1

我正在尝试创建一个动态 GM 脚本。这是我认为会做的

win = window.open('myScript.user.js');
win.document.writeln('// ==UserScript==');
win.document.writeln('// @name          sample script');
win.document.writeln('// @description   alerts hi');
win.document.writeln('// @include       http://www.google.com/*');
win.document.writeln('// ==/UserScript==');
win.document.writeln('');
win.document.writeln('(function(){alert("hi");})()');
win.document.close();

好吧,它没有。任何人都知道如何去做这件事?

4

1 回答 1

2

您不能使用 Greasemonkey(单独)动态创建 Greasemonkey 脚本。

GM 脚本不是 HTML 页面的一部分,因此将 GM 代码写入页面永远不会起作用。脚本需要安装到GM的脚本管理系统中。

GM 脚本无法写入文件系统,也无法访问足够的浏览器 chrome 来安装脚本插件。

  • 也许可以编写一个 GM 脚本,将其他脚本发布到服务器,然后将浏览器发送到该服务器。然后,GM 会提示用户安装新脚本。

  • 也许可以编写一个可以编写 GM 脚本的浏览器插件,但我怀疑这种方法会很困难。

  • 您可能可以编写一个 Python(或 C、VB 等)程序来生成用于安装的 GM 脚本。通过额外的工作,这样的程序也可能会自动安装脚本。


无论如何,您为什么要动态创建 Greasemonkey 脚本?可能有更简单的方法来实现真正的目标。?。



OP评论/澄清更新:

回复:"I want to be able to have a user select an element to get blocked and then create a script that sets that element's display to none on all sites from that domain"...

一种方法:

  1. 使用GM_setValue()存储域和选择器对。

  2. 该脚本首先会检查它是否为当前页面的域或 URL 存储了一个值(使用GM_getValue() 或 GM_listValues())。

  3. 如果找到匹配项,则隐藏选择器中指定的元素。


请注意,根据元素,出色的Adblock Plus 扩展可能能够更优雅地阻止元素(也节省带宽/DL 时间)。

于 2011-01-31T00:20:31.443 回答