我有 Greasemonkey 的问题,它不会自动更新我的脚本(可能是因为我们不想将它添加到它的 UserScripts 根目录,在这种情况下它似乎不会更新它)...
无论如何,我在我的脚本中添加了一段代码(主要思想来自 here )以检查脚本中的脚本版本并让用户知道是否有可用的新版本,就好像用户想要更新一样它,如果是这样,它应该为脚本 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 来安装它...
不知道有没有办法用这个方法更新脚本... 如果您分享您的知识并帮助我解决我的问题,我将不胜感激。
感谢您的宝贵时间!
附言我的firefox版本是15.0.1,Greasemonkey版本是1.1
最佳答案
不要忘记@grant
。
如果我错了请纠正我,但是你的脚本没有以 js 结尾。
var plugin_url = 'https://MyWebSiteURL/MonaTest.user.js?'+new Date().getTime();
尝试强制使用 .js 扩展名。
当我将日期添加到我的脚本时,GM 停止安装它。
删除末尾的日期并试一试。
最后一件事:您的脚本基于 2009 年的脚本。看看新的脚本,例如 this从 2011 年开始。
关于javascript - 如何在 greasemonkey 中更新用户脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12627139/