forked from Esryok/screeps-browser-ext
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuserscript.js
More file actions
185 lines (162 loc) · 4.32 KB
/
Copy pathuserscript.js
File metadata and controls
185 lines (162 loc) · 4.32 KB
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
177
178
179
180
181
182
183
184
185
const USERSCRIPT_START_MARKER = '==UserScript==';
const USERSCRIPT_END_MARKER = '==/UserScript==';
class Header {
/** @type {string} */
name;
/** @type {string} */
value;
index = 0;
/**
* @param {string} name
* @param {string} value
*/
constructor(name, value) {
this.name = name;
this.value = value;
}
}
class HeaderList {
/** @type {Header[]} */
headers = [];
/**
* @param {string} name
* @param {string} value
*/
add(name, value) {
this.headers.push(new Header(name, value))
}
/**
* @param {string} name
*/
#getHeaders(name) {
return this.headers.filter(h => h.name === name) ?? [];
}
/**
* @param {string} name
*/
get(name) {
const headers = this.#getHeaders(name);
if (headers.length !== 1) return null;
return headers[0].value ?? null;
}
/**
* @param {string} name
*/
getAll(name) {
const headers = this.#getHeaders(name);
const all = headers.map((h, idx) => { h.index = idx; return h; });
return all;
}
/**
* @param {string} name
* @param {string} value
* @param {number} [index]
*/
set(name, value, index = 0) {
let headers = this.#getHeaders(name);
if (headers.length === 0) {
return;
}
if (headers.length === 1) {
headers[0].value = value;
return;
}
if (index === undefined) {
throw new Error(`no index given for multi-header set`);
} else if (index < 0 || index >= headers.length) {
throw new Error(`index out of bounds for multi-header set`);
}
headers[index].value = value;
}
/**
* @param {object} [opts]
* @param {number} [opts.indentSize]
*/
output(opts) {
const { indentSize = 2 } = opts ?? {};
const prefix = "// ";
const lines = [];
lines.push(`${prefix}${USERSCRIPT_START_MARKER}`);
const longest = Math.max(...this.headers.map(h => h.name.length));
const columnWidth = Math.ceil((longest + 1) / indentSize) * indentSize;
for (const header of this.headers) {
const key = `@${header.name}`.padEnd(columnWidth + 1);
lines.push(`${prefix}${key}${header.value}`);
}
lines.push(`${prefix}${USERSCRIPT_END_MARKER}`);
return lines.join("\n") + "\n";
}
}
export class Userscript {
/** @type {HeaderList} */
#headers;
#script;
/**
*
* @param {string} contents
*/
constructor(contents) {
this.#headers = new HeaderList();
const end = contents.indexOf(USERSCRIPT_END_MARKER);
if (end === -1) {
console.info('marker end not found');
this.#script = contents;
return;
}
const nextNewline = contents.indexOf("\n", end);
if (nextNewline === -1) {
console.info('next newline not found');
this.#script = contents;
return;
}
const headers = contents.slice(0, nextNewline);
this.#script = contents.slice(nextNewline);
this._parseHeaders(headers);
}
/**
* @param {string} headerBlock
*/
_parseHeaders(headerBlock) {
let match;
let currentIndex = 0;
while ((match = /^\/\/\s+@(?<name>[-A-Za-z]*)\s+(?<value>.*)$/gmd.exec(headerBlock.slice(currentIndex))) !== null) {
const { name, value } = match.groups ?? {};
const [_startIndex, endIndex] = match.indices?.[0] ?? [0, 0];
this.#headers.add(name, value);
currentIndex += endIndex;
}
}
get headers() {
const self = this;
return {
/** @param {string} name */
get(name) {
return self.#headers.get(name);
},
/**
* @param {string} name
* @returns {[value: string, index: number][]}
*/
getAll(name) {
return self.#headers.getAll(name)?.map(h => [h.value, h.index]) ?? [];
},
/**
* @param {string} name
* @param {string} value
* @param {number} [index]
*/
set(name, value, index) {
self.#headers.set(name, value, index);
}
};
}
headerBlock() {
return this.#headers.output();
}
contents() {
return this.#script;
}
output() {
return this.headerBlock() + "\n" + this.#script;
}
}