油猴函数

油猴函数使用示例

使用油猴GM_*函数必须声明在沙盒环境中运行 @grant none

// @grant unsafeWindow

GM_addStyle & GM_getResourceText

// @grant GM_addStyle
// @grant GM_getResourceText
// @resource css https://cdn.jsdelivr.net/npm/index.css

GM_addStyle(GM_getResourceText(css))

GM_addElement

创建指定的 HTML 元素,应用所有给定的"属性"并返回注入的 HTML 元素 注意:此功能是实验性的,API 可能会更改

// @grant GM_addElement

GM_addElement('script', {
src: 'https://cdn.jsdelivr.net/npm/index.js',
type: 'text/javascript'
})

GM_addElement('link', {
href: 'https://cdn.jsdelivr.net/npm/index.css',
rel: 'stylesheet'
})

GM_set/get/deleteValue & GM_listValues

// @grant GM_setValue
// @grant GM_getValue
// @grant GM_listValues
// @grant GM_deleteValue

let value = "hello world"

GM_setValue("value", value) //将"名称"的值设置为存储

console.log(GM_getValue("value")) //从存储中获取"名称"的值 => hello world

console.log(GM_listValues()) //列出存储的所有名称 => ['value']

GM_deleteValue("value") //从存储中删除"名称"

console.log(GM_listValues()) // => []

GM_add/removeValueChangeListener

侦听储存name的值更改并返回更改前和后的值,回调函数的"remote"参数显示此值是从另一个选项卡的实例(true)还是在此脚本实例(false)中修改的。
因此,不同浏览器选项卡的脚本可以使用此功能相互通信

// @grant GM_setValue
// @grant GM_addValueChangeListener
// @grant GM_removeValueChangeListener

GM_setValue('value', 'old_value')

GM_addValueChangeListener('value', function (name, old_value, new_value, remote) {
console.log(name, old_value, new_value, remote) //3秒后输出 =>value old_value new_value false
})

setTimeout(() => {
GM_setValue('value', 'new_value')
}, 3000)

setTimeout(() => {
GM_removeValueChangeListener(add) //按 ID 删除侦听器
console.log('已删除');
GM_setValue('value', 'new_value1') //这里改变'value'的值后侦听器不会再执行
}, 6000)

GM_reg/unregisterMenuCommand

注册/删除一个菜单

// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand

let menu = GM_registerMenuCommand('hello world', function () {
alert('hello world')
GM_unregisterMenuCommand(menu) //按id删除一个菜单
}, 'h') //快捷键

GM_xmlhttpRequest

通过油猴脚本发送的XHR请求

// @grant GM_xmlhttpRequest

let ajax = GM_xmlhttpRequest({

method: "GET", //请求方法 GET POST

/* headers: { //消息头
"Content-Type": "application/x-www-form-urlencoded"
}, */

// data: '', //通过 POST 请求发送的字符串

// timeout: 10000, //超时(毫秒)

responseType: "json", //响应的数据类型 text arraybuffer blob document json

// overrideMimeType: "text/xml", //请求的 MIME 类型

url: 'url',

onabort: function () {
//如果请求中止,则要执行的回调
},
onerror: function () {
//如果请求最终出现错误,则要执行的回调
},
onloadstart: function () {
//在加载开始时执行的回调,如果 responseType 设置为"stream",则提供对流对象的访问
},
onprogress: function () {
//如果请求取得了一些进展,则要执行的回调
},
onreadystatechange: function () {
//在请求的就绪状态发生更改时要执行的回调
},
ontimeout: function () {
//如果请求由于超时而失败,则要执行的回调
},
onload: function (xhr) {
//如果加载了请求,则要执行的回调
console.log(xhr);
}
})
abort(ajax) //调用以取消此请求

GM_download

通过给定url下载文件到本地

// @grant GM_download

let down = GM_download({

url: 'url',

name: "文件名.后缀", //不填则自动获取文件名

saveAs: true, //布尔值,显示"保存为"对话框

onerror: function (error) {
//如果下载最终出现错误,则要执行的回调
console.log(error)
},
onprogress: (pro) => {
//如果此下载取得了一些进展,则要执行的回调
console.log(pro.loaded) //文件加载量
console.log(pro.totalSize) //文件总大小
},
ontimeout: () => {
//如果此下载由于超时而失败,则要执行的回调
},
onload: () => {
//如果此下载完成,则要执行的回调
}
})
abort(down) //调用以取消此下载

GM_notification & GM_openInTab & GM_setClipboard

// @grant GM_openInTab
// @grant GM_notification
// @grant GM_setClipboard

GM_notification({

title: "标题",

image: "图像链接",

text: "通知内容",

highlight: true, //布尔值,是否突出显示发送通知的选项卡

silent: false, //布尔值,是否播放声音

timeout: 10000, //设置通知隐藏时间

onclick: function () {

//在单击通知时调用

GM_openInTab("url", true) //使用此 URL打开一个新标签页

//or

GM_setClipboard("text") //将数据复制到剪贴板
},
ondone() {

//在通知关闭(无论是由超时还是单击触发)或突出显示选项卡时调用
}
})

posted @ 2022-03-16 16:05  marHangover  阅读(994)  评论(0)    收藏  举报