-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGoogleVoice.js
27 lines (22 loc) · 10.7 KB
/
GoogleVoice.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// ==UserScript==
// @name GithubVoiceNotifier
// @description 每三个月提醒 Google Voice 保活
// @author ladit
// @version 1.0.0
// @homepageURL https://github.com/ladit/Userscripts
// @supportURL https://github.com/ladit/Userscripts
// @url https://cdn.jsdelivr.net/gh/ladit/Userscripts@master/GoogleVoice.js
// ==/UserScript==
// Quantumult X:
// [task_local]
// 0 10 1 */3 * https://cdn.jsdelivr.net/gh/ladit/Userscripts@master/GoogleVoice.js, tag=GoogleVoice保活, enabled=true
// Surge:
// GoogleVoice保活 = type=cron, cronexp="0 10 1 */3 *", script-path=https://cdn.jsdelivr.net/gh/ladit/Userscripts@master/GoogleVoice.js
const $ = MagicJS('GithubVoiceNotifier')
$.notify('记得使用 Google Voice!', '', '', 'https://voice.google.com/')
$.done()
// https://github.com/blackmatrix7/MagicJS
/* eslint-disable */
// prettier-ignore
function MagicJS(a="MagicJS",b="INFO"){return new class{constructor(){if(this._startTime=Date.now(),this.version="2.2.3.7",this.scriptName=a,this.logLevels={DEBUG:5,INFO:4,NOTIFY:3,WARNING:2,ERROR:1,CRITICAL:0,NONE:-1},this.isLoon="undefined"!=typeof $loon,this.isQuanX="undefined"!=typeof $task,this.isJSBox="undefined"!=typeof $drive,this.isNode="undefined"!=typeof module&&!this.isJSBox,this.isSurge="undefined"!=typeof $httpClient&&!this.isLoon,this.node={request:void 0,fs:void 0,data:{}},this.iOSUserAgent="Mozilla/5.0 (iPhone; CPU iPhone OS 13_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Mobile/15E148 Safari/604.1",this.pcUserAgent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36 Edg/84.0.522.59",this._logLevel="INFO",this.logLevel=b,this._barkUrl="",this._barkKey="",this.isNode){this.node.fs=require("fs"),this.node.request=require("request");try{this.node.fs.accessSync("./magic.json",this.node.fs.constants.R_OK|this.node.fs.constants.W_OK)}catch(a){this.node.fs.writeFileSync("./magic.json","{}",{encoding:"utf8"})}this.node.data=require("./magic.json")}else this.isJSBox&&($file.exists("drive://MagicJS")||$file.mkdir("drive://MagicJS"),$file.exists("drive://MagicJS/magic.json")||$file.write({data:$data({string:"{}"}),path:"drive://MagicJS/magic.json"}))}set barkUrl(a){try{let b=a.replace(/\/+$/g,"");this._barkUrl=`${/^https?:\/\/([^/]*)/.exec(b)[0]}/push`,this._barkKey=/\/([^\/]+)\/?$/.exec(b)[1]}catch(a){this.logDebug("Bark config error.")}}set logLevel(a){let b=this.read("magicjs_loglevel");this._logLevel=b?b:a.toUpperCase()}get logLevel(){return this._logLevel}get isRequest(){return"undefined"!=typeof $request&&"undefined"==typeof $response}get isResponse(){return"undefined"!=typeof $response}get isDebug(){return"DEBUG"===this.logLevel}get request(){return"undefined"==typeof $request?void 0:$request}get response(){return"undefined"==typeof $response?void 0:($response.hasOwnProperty("status")&&($response.statusCode=$response.status),$response.hasOwnProperty("statusCode")&&($response.status=$response.statusCode),$response)}get platform(){return this.isSurge?"Surge":this.isQuanX?"Quantumult X":this.isLoon?"Loon":this.isJSBox?"JSBox":this.isNode?"Node.js":"Unknown"}read(a,b=""){let c="";this.isSurge||this.isLoon?c=$persistentStore.read(a):this.isQuanX?c=$prefs.valueForKey(a):this.isNode?c=this.node.data:this.isJSBox&&(c=$file.read("drive://MagicJS/magic.json").string);try{this.isNode&&(c=c[a]),this.isJSBox&&(c=JSON.parse(c)[a]),!b||("string"==typeof c&&(c=JSON.parse(c)),c=c&&"object"==typeof c?c[b]:null)}catch(d){this.logError(d),c=b?{}:null,this.del(a)}"undefined"==typeof c&&(c=null);try{if(!!c&&"string"==typeof c){var d=JSON.parse(c);"object"==typeof d&&d&&(c=d)}}catch(a){}return this.logDebug(`READ DATA [${a}]${b?`[${b}]`:""}(${typeof c})\n${JSON.stringify(c)}`),c}write(a,b,c=""){let d=c?{}:"";if(!!c&&(this.isSurge||this.isLoon)?d=$persistentStore.read(a):!!c&&this.isQuanX?d=$prefs.valueForKey(a):this.isNode?d=this.node.data:this.isJSBox&&(d=JSON.parse($file.read("drive://MagicJS/magic.json").string)),!!c){try{"string"==typeof d&&(d=JSON.parse(d)),d="object"==typeof d&&d?d:{}}catch(b){this.logError(b),this.del(a),d={}}this.isJSBox||this.isNode?((!d[a]||"object"!=typeof d[a])&&(d[a]={}),!d[a].hasOwnProperty(c)&&(d[a][c]=null),"undefined"==typeof b?delete d[a][c]:d[a][c]=b):"undefined"==typeof b?delete d[c]:d[c]=b}else this.isNode||this.isJSBox?"undefined"==typeof b?delete d[a]:d[a]=b:"undefined"==typeof b?d=null:d=b;"object"==typeof d&&(d=JSON.stringify(d)),this.isSurge||this.isLoon?$persistentStore.write(d,a):this.isQuanX?$prefs.setValueForKey(d,a):this.isNode?this.node.fs.writeFileSync("./magic.json",d):this.isJSBox&&$file.write({data:$data({string:d}),path:"drive://MagicJS/magic.json"}),this.logDebug(`WRITE DATA [${a}]${c?`[${c}]`:""}(${typeof b})\n${JSON.stringify(b)}`)}del(a,b=""){this.logDebug(`DELETE KEY [${a}]${!b?"":`[${b}]`}`),this.write(a,null,b)}notify(a=this.scriptName,b="",c="",d=""){if(d=(a=>{let b={};if("string"==typeof a)this.isLoon?b={openUrl:a}:this.isQuanX?b={"open-url":a}:this.isSurge&&(b={url:a});else if("object"==typeof a)if(this.isLoon)b.openUrl=a["open-url"]?a["open-url"]:"",b.mediaUrl=a["media-url"]?a["media-url"]:"";else if(this.isQuanX)b=a["open-url"]||a["media-url"]?a:{};else if(this.isSurge){let c=a["open-url"]||a.openUrl;b=c?{url:c}:{}}return b})(d),1==arguments.length&&(a=this.scriptName,b="",c=arguments[0]),this.logNotify(`title:${a}\nsubTitle:${b}\nbody:${c}\noptions:${"object"==typeof d?JSON.stringify(d):d}`),this.isSurge)$notification.post(a,b,c,d);else if(this.isLoon)d?$notification.post(a,b,c,d):$notification.post(a,b,c);else if(this.isQuanX)$notify(a,b,c,d);else if(this.isJSBox){let d={title:a,body:b?`${b}\n${c}`:c};$push.schedule(d)}this._barkUrl&&this._barkKey&&this.notifyBark(a,b,c)}notifyDebug(a=this.scriptName,b="",c="",d=""){"DEBUG"===this.logLevel&&(1==arguments.length&&(a=this.scriptName,b="",c=arguments[0]),this.notify(a,b,c,d))}notifyBark(a=this.scriptName,b="",c="",d=""){let e={url:this._barkUrl,headers:{"Content-Type":"application/json; charset=utf-8"},body:{title:a,body:b?`${b}\n${c}`:c,device_key:this._barkKey}};this.post(e,()=>{})}log(a,b="INFO"){this.logLevels[this._logLevel]<this.logLevels[b.toUpperCase()]||console.log(`[${b}] [${this.scriptName}]\n${a}\n`)}logDebug(a){this.log(a,"DEBUG")}logInfo(a){this.log(a,"INFO")}logNotify(a){this.log(a,"NOTIFY")}logWarning(a){this.log(a,"WARNING")}logError(a){this.log(a,"ERROR")}logRetry(a){this.log(a,"RETRY")}adapterHttpOptions(a,b){let c="object"==typeof a?Object.assign({},a):{url:a,headers:{}};c.hasOwnProperty("header")&&!c.hasOwnProperty("headers")&&(c.headers=c.header,delete c.header),!!c.headers&&"object"==typeof c.headers&&(!!c.headers["User-Agent"]||!!c.headers["user-agent"])||((!!!c.headers||"object"!=typeof c.headers)&&(c.headers={}),c.headers["User-Agent"]=this.isNode?this.pcUserAgent:this.iOSUserAgent);let d=!1;if(("object"==typeof c.opts&&(!0===c.opts.hints||!0===c.opts["Skip-Scripting"])||"object"==typeof c.headers&&!0===c.headers["X-Surge-Skip-Scripting"])&&(d=!0),d||(this.isSurge?c.headers["X-Surge-Skip-Scripting"]=!1:this.isLoon?c.headers["X-Requested-With"]="XMLHttpRequest":this.isQuanX&&("object"!=typeof c.opts&&(c.opts={}),c.opts.hints=!1)),(!this.isSurge||d)&&delete c.headers["X-Surge-Skip-Scripting"],!this.isQuanX&&c.hasOwnProperty("opts")&&delete c.opts,this.isQuanX&&c.hasOwnProperty("opts")&&delete c.opts["Skip-Scripting"],"GET"===b&&!this.isNode&&!!c.body){let a=Object.keys(c.body).map(a=>"undefined"==typeof c.body?"":`${encodeURIComponent(a)}=${encodeURIComponent(c.body[a])}`).join("&");0>c.url.indexOf("?")&&(c.url+="?"),c.url.lastIndexOf("&")+1!=c.url.length&&c.url.lastIndexOf("?")+1!=c.url.length&&(c.url+="&"),c.url+=a,delete c.body}return this.isQuanX?(c.hasOwnProperty("body")&&"string"!=typeof c.body&&(c.body=JSON.stringify(c.body)),c.method=b):this.isNode?(delete c.headers["Accept-Encoding"],"object"==typeof c.body&&("GET"===b?(c.qs=c.body,delete c.body):"POST"==b&&(c.json=!0,c.body=c.body))):this.isJSBox&&(c.header=c.headers,delete c.headers),c}adapterHttpResponse(a){let b={body:a.body,headers:a.headers,json:()=>JSON.parse(b.body)};return a.hasOwnProperty("statusCode")&&a.statusCode&&(b.status=a.statusCode),b}get(a,b){let c=this.adapterHttpOptions(a,"GET");this.logDebug(`HTTP GET: ${JSON.stringify(c)}`),this.isSurge||this.isLoon?$httpClient.get(c,b):this.isQuanX?$task.fetch(c).then(a=>{a.status=a.statusCode,b(null,a,a.body)},a=>b(a.error,null,null)):this.isNode?this.node.request.get(c,(a,c,d)=>{c=this.adapterHttpResponse(c),b(a,c,d)}):this.isJSBox&&(c.handler=a=>{let c=a.error?JSON.stringify(a.error):void 0,d="object"==typeof a.data?JSON.stringify(a.data):a.data;b(c,a.response,d)},$http.get(c))}getPromise(a){return new Promise((b,c)=>{magicJS.get(a,(a,d)=>{a?c(a):b(d)})})}post(a,b){let c=this.adapterHttpOptions(a,"POST");if(this.logDebug(`HTTP POST: ${JSON.stringify(c)}`),this.isSurge||this.isLoon)$httpClient.post(c,b);else if(this.isQuanX)$task.fetch(c).then(a=>{a.status=a.statusCode,b(null,a,a.body)},a=>{b(a.error,null,null)});else if(this.isNode){let a=this.node.request.post(c,b);a.status=a.statusCode,delete a.statusCode}else this.isJSBox&&(c.handler=a=>{let c=a.error?JSON.stringify(a.error):void 0,d="object"==typeof a.data?JSON.stringify(a.data):a.data;b(c,a.response,d)},$http.post(c,{}))}done(a={}){this._endTime=Date.now();let b=(this._endTime-this._startTime)/1e3;magicJS.logDebug(`SCRIPT COMPLETED: ${b}S.`),"undefined"!=typeof $done&&$done(a)}isToday(a){if(null==a)return!1;else{let b=new Date;return"string"==typeof a&&(a=new Date(a)),b.getFullYear()==a.getFullYear()&&b.getMonth()==a.getMonth()&&b.getDay()==a.getDay()}}isNumber(a){return"NaN"!==parseFloat(a).toString()}attempt(a,b=null){return a.then(a=>[null,a]).catch(a=>(this.logError(a),[a,b]))}retry(a,b=5,c=0,d=null){return(...e)=>new Promise((f,g)=>{function h(...e){Promise.resolve().then(()=>a.apply(this,e)).then(a=>{"function"==typeof d?Promise.resolve().then(()=>d(a)).then(()=>{f(a)}).catch(a=>{1<=b?0<c?setTimeout(()=>h.apply(this,e),c):h.apply(this,e):g(a),b--}):f(a)}).catch(a=>{this.logRetry(a),1<=b&&0<c?setTimeout(()=>h.apply(this,e),c):1<=b?h.apply(this,e):g(a),b--})}h.apply(this,e)})}formatTime(a,b="yyyy-MM-dd hh:mm:ss"){var c={"M+":a.getMonth()+1,"d+":a.getDate(),"h+":a.getHours(),"m+":a.getMinutes(),"s+":a.getSeconds(),"q+":Math.floor((a.getMonth()+3)/3),S:a.getMilliseconds()};for(let d in /(y+)/.test(b)&&(b=b.replace(RegExp.$1,(a.getFullYear()+"").substr(4-RegExp.$1.length))),c)new RegExp("("+d+")").test(b)&&(b=b.replace(RegExp.$1,1==RegExp.$1.length?c[d]:("00"+c[d]).substr((""+c[d]).length)));return b}now(){return this.formatTime(new Date,"yyyy-MM-dd hh:mm:ss")}today(){return this.formatTime(new Date,"yyyy-MM-dd")}sleep(a){return new Promise(b=>setTimeout(b,a))}}(a)}
/* eslint-enable */