繁体   English   中英

如何在greasemonkey中更新用户脚本

[英]How to update user script in greasemonkey

我有Greasemonkey的问题,它不会自动更新我的脚本(可能是因为我们不想将它添加到它的UserScripts根目录,在这种情况下它似乎不会更新它)...

反正我是添加一段代码(从主要思想在这里 )我的脚本来检查脚本中的脚本版本,让用户知道是否有新版本可用,并且如果用户想要更新它,如果是,则应该为脚本url打开一个新的窗口/选项卡(它应该触发Greasemonkey来安装它)...这是我的senario,它完美到了它应该打开一个新的窗口/标签...

在这里你可以看到我正在使用的功能:

function checkForUpdate(in_vid){

    var plugin_url = 'https://MyWebSiteURL/MonaTest.user.js?'+new Date().getTime();

    if ((parseInt(GM_getValue('SUC_last_update', '0')) + 86400000 <= (new Date().getTime()))){
        try {
            GM_xmlhttpRequest( {
                method: 'GET',
                url: plugin_url,
                headers: {'Cache-Control': 'no-cache'},
                onload: function(resp){
                    var local_version, remote_version, rt, script_name;

                    rt=resp.responseText;
                    GM_setValue('SUC_last_update', new Date().getTime()+'');
                    remote_version = parseFloat(/@version\s*(.*?)\s*$/m.exec(rt)[1]);
                    local_version = parseFloat(GM_getValue('SUC_current_version', '-1'));

                    if(local_version!=-1){
                        script_name = (/@name\s*(.*?)\s*$/m.exec(rt))[1];
                        GM_setValue('SUC_target_script_name', script_name);

                        if (remote_version > local_version){

                            if(confirm('There is an update available for the Greasemonkey script "'+script_name+'."\nWould you like to install it now?')){
                    ------->    GM_openInTab(plugin_url);
                                //window.open(plugin_url,'_blank')
                                //location.assign(plugin_url);
                                GM_setValue('SUC_current_version', remote_version);
                            }
                        }
                        else{
                            GM_log('No update is available for "'+script_name+'"');
                        }
                    }
                    else{
                        GM_setValue('SUC_current_version', remote_version+'');
                    }
                }
            });
        }
        catch (err){
            GM_log('An error occurred while checking for updates:\n'+err);
        }
    }
}

我尝试使用GM_openInTab,但它在控制台中返回此错误:

Timestamp: 9/27/12 9:55:33 AM
Error: ReferenceError: GM_openInTab is not defined
Source File: file:///Users/Mona/Library/Application%20Support/Firefox/Profiles/tonwb5lg.default/gm_scripts/MonaTest/MonaTest.user.js
Line: 97

我找不到任何引用来表明GM_openInTab不再支持了!

我尝试了其他解决方案,使用window.open和location.assign ...它们都不起作用,因为它们显示脚本源代码而不触发Greasemonkey来安装它...

我不知道是否有办法使用这种方法更新脚本...如果您分享您的知识并帮助解决我的问题,我将不胜感激。

谢谢你的时间!

PS我的firefox版本是15.0.1,Greasemonkey版本是1.1

别忘了@grant


如果我错了,请纠正我,但你的脚本不以js结尾。

var plugin_url = 'https://MyWebSiteURL/MonaTest.user.js?'+new Date().getTime();

尝试强制.js扩展名。
当我将日期添加到我的脚本时,GM停止安装它。
删除最后的日期并试一试。

最后一两件事:你在一个脚本基础脚本从2009年就来看看一个新的,像这样从2011年。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM