Greasymonkey 4.0+ 和 Tampermonkey 几个兼容方法
tampermonkey吧
全部回复
仅看楼主
level 5
Anix2012 楼主
GM_setValue GM_getValue GM_addStyle 兼容方法
2017年12月17日 12点12分 1
level 5
Anix2012 楼主
GM 4.0 发布以后,API有所改变.很多旧脚本不兼容了.
而且 脚本 需要针对 GM 和 TM 分别写,
这里提供几个方法的兼容函数,以便脚本统一适用.
包括 GM_setValue,GM_getValue,GM_addStyle
本经验发布于2017年12月17日,若日期相隔太远可能不适合.
2017年12月17日 12点12分 2
level 5
Anix2012 楼主
Greasy monkey 4.0+ API 变化
GM_setValue ==>> GM.setValue
GM_getValue ==>> GM.getValue
GM_addStyle ==>> 移除
注意: GM.setValue 和 GM.getValue 返回值都是 Promise 对象
2017年12月17日 12点12分 3
level 5
Anix2012 楼主
GM_setValue 兼容方法:
/* 兼容 Tamper monkey | Violent monkey | Greasy monkey 4.0+
* 为了兼容Greasy Monkey 4.0 储存结构化数据,比如 json Array 等,
* 应当先将对象字符串化,
* GMsetValue(name, JSON.stringify(defaultValue))
*/
function GMsetValue(name, defaultValue) {
if (typeof GM_setValue === 'function') {
GM_setValue(name, defaultValue);
return new Promise((resolve, reject) => {
resolve();
reject();
});
} else {
return GM.setValue(name, defaultValue);
}
}
2017年12月17日 12点12分 4
level 5
Anix2012 楼主
GM_getValue 兼容方法:
/* 兼容 Tamper monkey | Violent monkey | Greasy monkey 4.0+
* 为了兼容Greasy Monkey 4.0 获取结构化数据,比如 json Array 等,
* 应当先将字符串还原为对象,再执行后续操作
* GMgetValue(name,defaultValue).then((result)=>{
* let result = JSON.parse(result);
* // other code...
* };
*/
function GMgetValue(name, defaultValue) {
if (typeof GM_getValue === 'function') {
return new Promise((resolve, reject) => {
resolve(GM_getValue(name, defaultValue));
// reject();
});
} else {
return GM.getValue(name, defaultValue);
}
}
2017年12月17日 12点12分 5
level 5
Anix2012 楼主
GM_addStyle 兼容方法:
/* 兼容 Tamper monkey | Violent monkey | Greasy monkey 4.0+
* css 是CSS文本
* 这个方法可以应用到打开本地文件(file://)中添加样式,
* 也可以避免局部更新文档内容的时候把样式去掉(比如某度搜索)
* 总之目前来说,比Tamper monkey 的 GM_addStyle 效果要好
*/
function GMaddStyle(css) {
let a = document.createElement('style'),doc;
a.textContent = '<!--\n' + css + '\n-->';
if (location.origin === "file://") {
doc = document.head || document.documentElement;
} else {
doc = document.body || document.documentElement;
}
doc.appendChild(a);
}
2017年12月17日 12点12分 6
方法有更新,看10楼
2018年01月02日 08点01分
level 5
Anix2012 楼主
因为新版 Greasy monkey 没有办法直接存储结构化数据,所以要用 json 字符串转化和反转化方法来操作.
因为返回的是 Promise 对象,所以需要用到 GMgetValue 的值得处理逻辑应该放在 then 中来处理
2017年12月17日 12点12分 7
level 5
Anix2012 楼主
去 TM™ 的 百度经验,老子写上面这篇经验,搞来搞去都有 敏感词雷区...
不发经验了,就发贴吧这里吧
2017年12月17日 12点12分 8
level 1
good ,我这两天正郁闷,这资料是哪里来的?我好像在官网都没看到
2017年12月21日 06点12分 9
level 5
Anix2012 楼主
GM_addStyle 最新方法
/*此方法无毒无副作用,尽显雄风,效果持久
* 这个方法可以应用到打开本地文件(file://)中添加样式,
* 也可以避免局部更新文档内容的时候把样式去掉(比如百度搜索)
* 总之目前来说,比Tamper monkey 的 GM_addStyle 效果要好
*/
function addStyle(cssStr){
try {
let node = document.createElement('style');
node.textContent = cssStr;
document.querySelector(':root').appendChild(node);
} catch(e){}
}
2018年01月02日 08点01分 10
这个方法,今日发现一点点小问题,就是 CSP (内容安全策略) 页面,加载一些字体.可能无效.比如 GitHub, 想要加载自定义字体德尔时候,就会被拒绝.... 但是大多数站点没有设置csp,还是挺好用的.
2018年01月05日 18点01分
level 1
你好 您能破解网站VIP视频吗 有偿,能的话联系我谢谢 qq 1324417340
2018年01月21日 06点01分 11
不能
2018年04月18日 13点04分
1