-
Notifications
You must be signed in to change notification settings - Fork 2
/
toNoTM.js
42 lines (40 loc) · 1.74 KB
/
toNoTM.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
const fs = require("fs")
const request = require("superagent")
const toString = require('stream-to-string')
const B64 = require('@hapi/b64')
const encoder = new B64.Encoder()
Promise.all([
getRemoteResources(fs.readFileSync("./b站时间线筛选.user.js").toString()),
request.get("https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js")
]).then(data => fs.writeFileSync("./NoTM.js",data[1].body.toString() + "\n\n\n\n" + data[0].replace("\nvar $ = unsafeWindow.jQuery\n","")))
function getRemoteResources(script) {
let resources = {}
let infoBlock = script.split("\n// ==/UserScript==")[0].split("// ==UserScript==\n").pop().split("\n")
return Promise.all(infoBlock.filter(l => l.startsWith("// @require ")).map(l => {
let url = l.replace("// @require ","").trim()
console.log("getting script: " + url)
return request.get(url).then(res => res.body.toString())
})).then(rs => {
rs.push(script)
return rs.join("\n\n\n\n")
}).then(sip => {
return Promise.all(infoBlock.filter(l => l.startsWith("// @resource ")).map(l => {
let pair = l.replace("// @resource ","").split(" ").filter(p => p != "")
let key = pair[0]
let url = pair.pop()
console.log("getting " + key + ": " + url)
let resource = {}
return Promise.all([
request.head(url).then(res => resource.type = res.type),
toString(request.get(url).pipe(encoder)).then(b => resource.base64 = b)
]).then(() => resources[key] = resource)
})).then(() => {
return "var GM = {}\n\
!function(GM){\n\
let resources = " + JSON.stringify(resources) + "\n\
GM.getResourceUrl = key => Promise.resolve('data:' + resources[key].type +';base64,' + resources[key].base64)\n\
return GM\n\
}(GM)\n\n\n\n" + sip
})
})
}