动态创建greasemonkey脚本
我正在尝试创建一个动态 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法使用 Greasemonkey(单独)动态创建 Greasemonkey 脚本。
GM 脚本不是 HTML 页面的一部分,因此将 GM 代码写入页面永远不会起作用。该脚本需要安装到GM的脚本管理系统中。
GM 脚本无法写入文件系统,也无法访问足够的浏览器镶边来安装脚本插件。
您也许能够编写一个 GM 脚本,将其他脚本发布到服务器,然后将浏览器发送到该服务器。然后,GM 将提示用户安装新脚本。
您也许能够编写一个可以编写 GM 脚本的浏览器插件,但我怀疑这种方法会很困难。
您可能可以编写一个 Python(或 C、VB 等)程序来生成用于安装的 GM 脚本。通过额外的工作,这样的程序也可能会自动安装脚本。
无论如何,为什么要动态创建 Greasemonkey 脚本呢?也许有更简单的方法来实现真正的目标。?
OP评论/澄清的更新:
回复:
“我希望能够让用户选择要阻止的元素,然后创建一个脚本,将该元素的显示设置为在该域的所有网站上均不显示”
...一种方法:
使用 GM_setValue() 存储域和选择器对。
脚本首先会检查是否为当前页面的域或 URL 存储了值(使用 GM_getValue() 或 GM_listValues())。
如果找到匹配项,则隐藏选择器中指定的元素。
请注意,根据元素的不同,优秀的 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:
Store domain and selector pairs using GM_setValue().
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()).
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).