TampermonkeyJS开发笔记
Contents
引入jquery等第三方库
|
|
引入外部Css样式文件
暂未发现油猴如何像引入js一样引入css文件。以下是纯javascript的实现:
1 2 3 4 5 6 7 8 9 |
function loadStyles(url) {
var link = document.createElement("link");
link.type = "text/css";
link.rel = "stylesheet";
link.href = url;
var head = document.getElementsByTagName("head")[0];
head.appendChild(link);
}
loadStyles('https://cdn.bootcss.com/jqueryui/1.12.1/jquery-ui.min.css'); |
定义样式
授权
|
|
定义
|
|
使用
|
|
请求外部资源
使用GM_xmlhttpRequest(details)来实现对外的http请求。
授权部分:
|
|
代码部分:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
GM_xmlhttpRequest({
method: 'GET',
url: 'http://quote.eastmoney.com/config/sidemenu.json',
responseType: 'json', // arraybuffer, blob, json
onload: res => {
console.log(res);
if (res.status == 200) {
console.log(res.response);
}
},
onerror: err => {
console.error(err);
}
}) |
小技巧:绑定到unsafeWindow上
|
|
绑定到window对象上,这样在网页window命名空间中可以直接使用gmAjax,相当于调用GM_xmlhttpRequest。
Author Joyeah
LastMod 2019-12-04