【问题标题】:Create a "config" or "options" page for a Greasemonkey script为 Greasemonkey 脚本创建“配置”或“选项”页面
【发布时间】:2013-01-29 23:57:22
【问题描述】:

我写了一个简单的 Greasemonkey 脚本,我正在尝试为这个脚本创建一个“配置”页面(就像用于 Google Chrome 扩展的那个。)有什么方法可以创建一个配置页面对于用户脚本,例如 Google Chrome 扩展的“选项”页面?没有任何方法可以将 .html 页面包含为 Greasemonkey 脚本的一部分(据我所知),所以我正在寻找其他选项。

// ==UserScript==
// @name       Redirector
// @namespace  http://use.i.E.your.homepage/
// @version    0.1
// @description  enter something useful
// @match      http://*/*
// @copyright  2012+, You
// @run-at document-start
// ==/UserScript==

redirectToPage("http://www.youtube.com/", "http://www.google.com");

function redirectToPage(page1, page2){
if(window.location.href.indexOf(page1) != -1){
    window.location.href = page2;
}
}

【问题讨论】:

  • 也许我可以使用“about:blank”页面,例如:about:blank#redirector-config。这行得通吗? (我需要对其进行测试并找出答案。:))

标签: greasemonkey


【解决方案1】:

有一些库为用户脚本提供配置页面:

1) GM_config

2) MonkeyConfig

3) GM_registerMenuCommand Submenu JS Module


每个库的用法各不相同,但通常您授予他们需要的权限,例如 GM_getValueGM_setValue,并通过 @require 指令要求库,例如:

// ==UserScript==
// @name          My Userscript
// @description   An example userscript with a config page
// @version       0.0.1
// @require       https://www.example.com/lib/myconfig.js
// @grant         GM_getValue
// @grant         GM_setValue
// @grant         GM_addStyle
// @grant         GM_registerMenuCommand
// ==/UserScript==

const config = new MyConfig({ ... })

然后您注册一个打开配置页面/对话框的菜单命令,例如:

GM_registerMenuCommand('Configure My Userscript!', () => {
    config.open()
})

在 MonkeyConfig 的情况下,它可以为您注册命令:

const config = new MonkeyConfig({
    title: 'Configure My Userscript!',
    menuCommand: true,
    // ...
})

对于高级用途,配置器可能允许为关闭/取消/保存事件注册listeners,并提供对 CSS 和其他选项的控制。详细说明请见GM_config wikiMonkeyConfig homepage

【讨论】:

  • 感谢您提供有用而全面的答案。第一次使用GM_config。设置的简单性给我留下了深刻的印象。
【解决方案2】:

此用户脚本不使用任何外部库。通过修改你可以做出好的用户界面。

// ==UserScript==
// @name         UI development
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       ManojBhakarPCM
// @match        https://www.google.com/
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    function ui_create(contentss){
        var div = document.createElement('div');
        //var contentss = "<h1>TESTsssss</h1>";
        div.innerHTML = '<div id="mbpcm_modal" style="position: fixed;z-index: 1;left: 0;top: 0;width: 100%;height: 100%;overflow: auto;display:none;"><div id="mbpcm_content" style="background-color: pink;margin: 15% auto;padding: 20px;border: 1px solid black;width: 20%;box-shadow:20px 20px 30px 2px gray;border-radius:7px;"><span id="mbpcm_close" style="float: right;font-size:25px;cursor: pointer;">&times;</span>'+ contentss +'</div></div>';
        document.body.appendChild(div);
        var modal = document.getElementById("mbpcm_modal");
        var close = document.getElementById("mbpcm_close");
        close.onclick = function(){modal.style.display = "none";}
    }
    function ui_add_opener(parent,classs,stylee){
        var btn = document.createElement("button");
        btn.setAttribute("class",classs);
        btn.setAttribute("style",stylee);
        btn.setAttribute("id","mbpcm_ui_opener");
        btn.innerHTML = "Settings";
        parent.appendChild(btn);
        var modal = document.getElementById("mbpcm_modal");
        var btnn = document.getElementById("mbpcm_ui_opener");
        btnn.onclick = function(){modal.style.display = "block";}
    }
    function ui_addElement(el,cls,styl,val,innerHtml){
        var modal = document.getElementById("mbpcm_content");
        var e = document.createElement(el);
        e.setAttribute("class",cls);
        e.setAttribute("style",styl);
        e.setAttribute("value",val);
        e.innerHTML = innerHtml;
        modal.appendChild(e);
        return e;
    }

    ui_create("<h1>Title</h1><span>discription</span><br>");
    ui_add_opener(document.getElementById("gbw"),"RNmpXc","nothing");
    ui_addElement("button","modal","none","none","TestButton").onclick = function(){alert("hellow ji ");}
    ui_addElement("br","","","","");
    ui_addElement("button","none","none","none","TestButton").onclick = function(){alert("How are you");}
    ui_addElement("button","none","none","none","TestButton").onclick = function(){alert("Kaise ho");}
})();

【讨论】:

    【解决方案3】:

    如果您将它用于 chrome,那么它不是 Greasemonkey 而是 Tampermonkey。

    您可以考虑使用 GM_getResourceText,将您的 html 粘贴到 pastebin.com(或类似网站)并将链接作为 @resource 之一添加到元数据块。 至少,我知道它对 Greasemonkey 有效。

    例如:

    // @resource configHtml http://pastebin.com/raw.php?i=2RjKfwJQ
    
    // ... some DOM node that you will append to the current page
    node.innerHTML = GM_getResourceText("configHtml");
    

    【讨论】:

    • Greasemonkey 脚本和 Chrome 用户脚本之间有什么我应该注意的区别吗? ://
    • 原生 Chrome 用户脚本不支持 GM_* 函数。 Tampermonkey 支持其中的大多数。
    【解决方案4】:

    这是非常需要的,但现在两种方法的组合应该可以工作。

    1) 对于个人用途,我在脚本顶部只有一堆变量。这里的问题是,如果其他人使用我的脚本,更新最终会删除他的偏好。

    2) 在您的网站上有一个配置页面。虽然这非常有效,但网站总是被删除。脚本没有充分的理由依赖网站来工作。

    如果您同时执行这两项操作,则用户可以在脚本网站消失时编辑脚本中的首选项。

    这是一个示例,其中不需要的功能被注释掉了。

    http://go-here.nl/gm/wikipedia-clean-up.php

    祝你好运并享受

    【讨论】:

    【解决方案5】:

    对已包含的页面使用参数,如果已设置,则清除整个文档: http://page.my.script.runs.on/?configPage=true

    if(getUrlParameter("configPage") === "true") {
        $(document).empty
    }
    

    【讨论】:

    • $(document).empty 什么都不做。为什么还要依赖jQuery,还是这个伪代码?使用内置方法:document.body.replaceChildren([]);。可以使用new URL(location.href).searchParams.get("configPage") 代替getUrlParameter("configPage")
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-07
    相关资源
    最近更新 更多