-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
dev.user.js
73 lines (71 loc) · 2.02 KB
/
dev.user.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// ==UserScript==
// @name rollup-userscript-template [dev]
// @description Bundle typescript, react and JSX/TSX script files into a single userscript file with rollup
// @namespace github.com/cvzi
// @require https://unpkg.com/react@18/umd/react.development.js
// @require https://unpkg.com/react-dom@18/umd/react-dom.development.js
// @match https://github.com/*
// @version 1.3.0
// @homepage https://github.com/cvzi/rollup-userscript-template
// @author cuzi
// @license MIT
// @grant GM.getValue
// @grant GM.xmlHttpRequest
// @grant GM.setValue
// ==/UserScript==
/* globals GM */
'use strict';
(function () {
const url = `http://localhost:8124/bundle.user.js?${Date.now()}`
new Promise(function loadBundleFromServer (resolve, reject) {
const req = GM.xmlHttpRequest({
method: 'GET',
url: url,
onload: function (r) {
if (r.status !== 200) {
return reject(r)
}
resolve(r.responseText)
},
onerror: e => reject(e)
})
if (req && 'catch' in req) {
req.catch(e => { /* ignore */ })
}
}).catch(function (e) {
const log = function (obj, b) {
let prefix = 'loadBundleFromServer: '
try {
prefix = GM.info.script.name + ': '
} catch (e) {}
if (b) {
console.log(prefix + obj, b)
} else {
console.log(prefix, obj)
}
}
if (e && 'status' in e) {
if (e.status <= 0) {
log('Server is not responding')
GM.getValue('scriptlastsource3948218', false).then(function (src) {
if (src) {
log('%cExecuting cached script version', 'color: Crimson; font-size:x-large;')
/* eslint-disable no-eval */
eval(src)
}
})
} else {
log('HTTP status: ' + e.status)
}
} else {
log(e)
}
}).then(function (s) {
if (s) {
/* eslint-disable no-eval */
eval(`${s}
//# sourceURL=${url}`)
GM.setValue('scriptlastsource3948218', s)
}
})
})()