forked from rattboi/dotfiles
/
bootstrap-code-injector.js
176 lines (166 loc) · 5.47 KB
/
bootstrap-code-injector.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// This takes the custom injections (css and js) I have and writes them to
// bootstrap.js inside of the Injector.safariextension repository. The contents
// of the file implicitly is written to the localstorage at startup time. I've
// added a script tag that imports the file before storage.js runs.
// This script is mostly complete. Last time I used it, I needed to save an
// injection to get injections actually working, even though there were no
// changes. bootstrap.js might need to get imported last and then run some
// function that applies the injections.
const fs = require('fs')
const path = require('path')
const loader = require('./load-injector-data.js')
const preCssIncludes = require('./apply-global-pre-css.js')
/**
* The old injector style uses wildcards (*), whereas the new code-injector uses
* regex. We need to convert uses of . and * to an escaped . and * needs to
* become ".*". There may be other regex-isms to address. Forward slash (/)
* should be okay, since these are fed in via new Regex, and the / is for
* literals.
*/
const wildcardToRegex = (s) => {
return s.replace(/\./g, '\\.').replace(/\*/g, '.*').replace(/\?/g, '.?')
}
const rules = loader().map(m => {
return [
{
_version: '2',
// The code is read as a string and presumably processed or simply used
// later.
code: {
css: m.styles,
js: m.script,
},
// type: 'css',
enabled: true,
options: {
enabled: true,
onLoad: m.name == 'preload',
topFrameOnly: false,
},
selector: wildcardToRegex(
(m.name == 'preload' ? preCssIncludes() : m.includes).join('|'),
),
// path: null,
// Local files are read from disk, but we want to be able to process them
// first. So set this to false and the system will accept the rule's
// script or css as-is.
local: false,
// path: path.resolve(`./custom-css/${m.name}.css`),
// local: true,
// local: true,
/**
* We want CSS to be loaded afterwards, otherwise the event doesn't fire
* for simple navigation actions and then requires a page refresh. Waiting
* for the assets to load leaves an annoying white flash though. To remedy
* this we have a preload CSS file that sets the background to something
* tolerable right from the start.
*/
onLoad: m.name == 'preload',
/**
* Make this rule apply to any frame matching the URL, or just that a rule
* also is injected into the frame regardless of the frame's URL?
*/
topFrameOnly: false,
},
// {
// // The code is read as a string and presumably processed or simply used
// // later.
// code: m.script,
// type: 'js',
// enabled: true,
// options: {
// enabled: true,
// onLoad: m.name == 'preload',
// topFrameOnly: false,
// },
// selector: wildcardToRegex(m.includes.join('|')),
// // path: null,
// // Local files are read from disk, but we want to be able to process them
// // first. So set this to false and the system will accept the rule's
// // script or css as-is.
// local: false,
// // path: path.resolve(`./custom-css/${m.name}.js`),
// // local: true,
// onLoad: true,
// /**
// * Make this rule apply to any frame matching the URL, or just that a rule
// * also is injected into the frame regardless of the frame's URL?
// */
// topFrameOnly: false,
// },
]
})
// Flattens the list.
.reduce((xs, ys) => xs.concat(ys), [])
// This is a failed approach at writing to one of the files directly. However
// the system loads from the local store anyways.
//
// fs.readFile(
// path.resolve('../code-injector/dist/script/background.js'),
// 'utf8',
// (error, data) => {
// if(error) {
// console.error('Error reading file', error)
// process.exit(1)
// } else {
// const modified = data.replace(
// 'var rules = [];',
// `var rules = ${JSON.stringify(rules)};`,
// )
// fs.writeFileSync(
// path.resolve('../code-injector/dist/script/background.js'),
// modified,
// )
// console.log('Rewrote background.js')
// }
// })
const code = `\
browser.storage.local.set({rules: ${JSON.stringify(rules)}})
console.log('Loaded rules! v2')
`
fs.writeFileSync(
path.resolve('../code-injector/script/bootstrap.js'),
code,
)
// As part of this, write out a file we can import in case we need to bootstrap
// the system.
// Is this even used? NOPE!
const exportableRules = loader().map(m => {
return [
{
_version: '2',
description: 'Generated by bootstrap-code-injector.js',
enabled: true,
onLoad: false,
topFrameOnly: false,
// Duplicated because v2 says so, but I don't think it's implemented yet.
options: {
enabled: true,
onLoad: false,
topFrameOnly: false,
},
selector: wildcardToRegex(m.includes.join('|')),
code: {
js: '',
css: '',
html: '',
files: [
{
type: 'local',
ext: 'css',
path: path.resolve(`./custom-css/${m.name}.css`),
},
{
type: 'local',
ext: 'js',
path: path.resolve(`./custom-css/${m.name}.js`),
},
],
},
},
]
})
fs.writeFileSync(
path.resolve('./code-injector-rules.json'),
JSON.stringify(exportableRules),
)