3

我对 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

4

1 回答 1

1

别忘了@grant


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

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

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

最后一件事:你的剧本是基于 2009 年的剧本。看看一个新的,比如2011年的剧本。

于 2012-09-27T18:04:19.440 回答