动态创建greasemonkey脚本

发布于 2024-10-15 04:22:39 字数 498 浏览 2 评论 0原文

我正在尝试创建一个动态 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();

,但事实并非如此。有人知道如何去做这件事吗?

I'm trying to create a dynamic GM script. Here's what I thought would do it

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();

Well it doesn't. Anyone have any ideas how to go about doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零度℉ 2024-10-22 04:22:39

您无法使用 Greasemonkey(单独)动态创建 Greasemonkey 脚本。

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

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

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

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

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


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



OP评论/澄清的更新:

回复:“我希望能够让用户选择要阻止的元素,然后创建一个脚本,将该元素的显示设置为在该域的所有网站上均不显示”...

一种方法:

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

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

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

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

You cannot dynamically create Greasemonkey scripts with Greasemonkey (alone).

A GM script is not part of the HTML page, so writing GM code to a page will never work. The script needs to be installed into GM's script management system.

A GM script cannot write to the file system, nor access sufficient browser chrome to install a script add-on.

  • You might be able to write a GM script that posts other scripts to a server, and then sends the browser to that server. GM would then prompt the user to install the new script.

  • You might be able to write a browser add-on that could write GM scripts, but I suspect that this approach will be difficult.

  • You probably could write a Python (or C, VB, etc.) program that generates GM scripts for installation. With extra work, such a program could probably automatically install the script, too.


Why do you want to dynamically create Greasemonkey scripts, anyway? There may be a simpler method to accomplish the true goal.?.



Update for OP comment/clarification:

Re: "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"...

One way to do that:

  1. Store domain and selector pairs using GM_setValue().

  2. The script would, first thing, check to see if it had a value stored for the current page's domain or URL (using GM_getValue() or GM_listValues()).

  3. If a match was found, hide the element(s) as specified in the selector.

Note that, depending on the element, the excellent Adblock Plus extension may be able to block the element much more elegantly (saves bandwidth/DL-time too).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文