util
for (const [name, t] of [['GM_log', typeof GM_log], ['GM_xmlhttpRequest', typeof GM_xmlhttpRequest], ['GM_getValue', typeof GM_getValue], ['GM_setValue', typeof GM_setValue]]) {
if (t === "undefined") {
const s = `${name}不存在!请检查是否声明@grant ${name}`
console.log(`%c${s}`, `color: #f0ad4e; font-size: 15px`)
}
}
const gmlog = Object.freeze({
fontSize: 16,
log(s, level) {
const levels = ['debug', 'info', 'warn', 'error']
if (typeof GM_log === 'function') {
GM_log(`${s}`, levels.includes(`${level}`.toLowerCase()) ? level : 'debug')
} else {
console.log(`%cGM_log函数不存在,请检查是否申明@grant GM_log`, 'color: red; background: black; font-size: 18px')
}
},
debug(s) {
console.log(`%c${s}`, `color: #aaaaaa; font-size: ${this.fontSize}px`)
this.log(s, 'debug')
},
info(s) {
console.log(`%c${s}`, `color: #5bc0de; font-size: ${this.fontSize}px`)
this.log(s, 'info')
},
success(s) {
console.log(`%c${s}`, `color: #22bb33; font-size: ${this.fontSize}px`)
this.log(s, 'info')
},
warn(s) {
console.log(`%c${s}`, `color: #f0ad4e; font-size: ${this.fontSize}px`)
this.log(s, 'warn')
},
error(s) {
console.log(`%c${s}`, `color: #bb2124; font-size: ${this.fontSize}px`)
this.log(s, 'error')
}
})
const apiClient = Object.freeze({
request(method, url, options = {}) {
options = Object.assign({}, options)
switch (method) {
case 'GET':
case 'HEAD': {
if (typeof options.params === 'object') {
const params = Object.assign({}, options.params)
const urlobj = new URL(url)
for (const [k, v] of Object.entries(params)) {
urlobj.searchParams.set(k, v)
}
url = urlobj.toString()
delete options['params']
}
break
}
case 'POST': {
if (typeof options.data === 'object') {
const data = Object.assign({}, options.data)
const formData = new FormData()
for (const [k, v] of Object.entries(data)) {
formData.append(k, v)
}
options.data = formData
}
break
}
}
return new Promise((resolve, reject) => {
options = Object.assign(options, {
method: method,
url: url,
onload: function (res) {
let respJson = undefined
try {
respJson = JSON.parse(res.responseText)
} catch (e) { }
resolve(Object.assign(res, { json: respJson }))
},
onerror: e => {
reject(`请求: ${url} 出错了:${e}`)
},
ontimeout: () => {
reject(`请求: ${url} 已超时`)
},
onabort: () => {
reject(`请求: ${url} 已终止`)
}
})
GM_xmlhttpRequest(options)
})
},
okrequest(method, url, options = {}) {
return new Promise((resolve, reject) => {
this.request(method, url, options).then(resp => {
errorMsg = ''
if (resp.status >= 400 && resp.status < 500) {
errorMsg = `${url} ${resp.status} 客户端错误:${resp.responseText || ''}`
} else if (resp.status >= 500 && resp.status < 600) {
errorMsg = `${url} ${resp.status} 服务器错误:${resp.responseText || ''}`
}
if (errorMsg) {
reject(errorMsg)
} else {
resolve(resp)
}
}).catch(e => {
reject(e)
})
})
},
get(url, options = {}) {
return this.request('GET', url, options)
},
head(url, options = {}) {
return this.request('HEAD', url, options)
},
post(url, options = {}) {
return this.request('POST', url, options)
},
okget(url, options = {}) {
return this.okrequest('GET', url, options)
},
okhead(url, options = {}) {
return this.okrequest('HEAD', url, options)
},
okpost(url, options = {}) {
return this.okrequest('POST', url, options)
}
})
const storageManager = Object.freeze({
getItem(key) {
let record = {}
try {
record = JSON.parse(GM_getValue(key))
} catch (error) {
record = {}
}
return Object.assign({}, record instanceof Array ? {} : record)
},
setItem(key, obj) {
GM_setValue(key, JSON.stringify(obj))
},
itemExists(key) {
return GM_getValue(key) !== undefined
},
getItemProxy(itemKey) {
const getItem = this.getItem
const setItem = this.setItem
const record = getItem(itemKey)
return new Proxy(record, {
set(target, key, value) {
const isDelete = [null, undefined].includes(value)
const changed = isDelete || target[key] !== value
if (!changed) {
return true
}
const data = getItem(itemKey)
if (isDelete) {
delete target[key]
delete data[key]
} else {
target[key] = value
data[key] = value
}
setItem(itemKey, data)
return true
}
})
}
})