-
Notifications
You must be signed in to change notification settings - Fork 18
/
update-css-fixes.js
147 lines (140 loc) · 5.81 KB
/
update-css-fixes.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
// jshint esversion: 8
// jshint unused: true
let fs = require('fs');
(()=>{
'use strict';
console.log('Loading filters...');
let cssFileName = './css-fixes-experimental.txt';
let fixesFileName = './ruadlist-fixes.user.css';
let templateFileName = `${fixesFileName}.template`;
let fileEncoding = 'utf8';
let oldVersion = null;
{
let fd = fs.openSync(fixesFileName, 'r');
let len = 256, buff = Buffer.alloc(len, ' ', fileEncoding);
fs.readSync(fd, buff, 0, len, 0);
oldVersion = /@version\s+(\d+\.\d+\.\d+)[\r\n]+/.exec(buff.toString())[1];
fs.closeSync(fd);
}
let version = null;
console.log('Current version:', oldVersion);
{
let ctime = fs.statSync(cssFileName).ctime;
let fmt = x => `${(x < 10 ? '0' : '')}${x}`;
let [yr, mn, dy] = [ctime.getFullYear(), ctime.getMonth() + 1, ctime.getDate()];
let [hr, mi] = [ctime.getHours(), ctime.getMinutes()];
version = `0.${yr}${fmt(mn)}${fmt(dy)}.${hr > 0 ? hr : ''}${hr > 0 ? fmt(mi) : mi}`;
}
console.log('New version:', version);
if (!version || !oldVersion)
throw `Unable to determine version. Exit.`;
if (oldVersion === version) {
console.log(`Source didn't change. Exit.`);
return;
}
let data = fs.readFileSync(cssFileName, fileEncoding);
let template = fs.readFileSync(templateFileName, fileEncoding);
if (!data || !template)
throw 'Failed to read files!';
template = template.replace('%version%', version);
console.log('Parsing filters...');
let filters = data.split(/[\r\n]+/);
let filterParts = /^([-\w.,]+)#\??#(.+?)(\:style\((.+)\))?\s*$/;
let isDeep = /\/deep\//;
let styles = [];
let strcomp = (a, b) => a == b ? 0 : (a < b ? -1 : 1);
{
let domains = new Map(),
skipped = [],
i = 0, idx = 0;
for (let filter of filters) {
let parts = filterParts.exec(filter);
if (!parts)
continue;
let [rule, location, selector, skip, style] = parts;
style = style || 'display: none !important';
if (!(location && selector) || isDeep.test(selector)) {
skipped.push(filter);
continue;
}
if (!domains.has(location)) {
domains.set(location, i);
i++;
}
idx = domains.get(location);
if (!styles[idx]) {
styles[idx] = {
location: location,
rules: [{ selector: selector, style: style}]
};
} else {
styles[idx].rules.push({ selector: selector, style: style});
}
}
console.log(`Skipped rows:\n> ${skipped.join('\n> ')}`);
}
styles.sort((a, b) => strcomp(a.location, b.location));
console.log('Domain blocks:', styles.length);
console.log('Generating CSS code...');
{
let css = '';
let rulecomp = (a, b) => strcomp(a.style, b.style) * 2 + strcomp(a.selector, b.selector);
// UserStyles doesn't accept some first-level domains from PeerName and similar DNS
let skipUnsupportedDomains = loc => !/\.lib$/.test(loc);
for (let style of styles) {
if (style.rules.length > 1) {
style.rules.sort(rulecomp);
let rules = [],
smap = new Map(),
i = 0, idx = 0;
for (let rule of style.rules) {
if (!smap.has(rule.style)) {
smap.set(rule.style, i);
i++;
}
idx = smap.get(rule.style);
if (!rules[idx]) {
rule.selector = [rule.selector];
rules[idx] = rule;
} else {
rules[idx].selector.push(rule.selector);
}
}
style.rules = rules;
}
style.location = style.location.split(',').filter(skipUnsupportedDomains);
// construct domains header
css += `\n@-moz-document${style.location.length > 1 ? '\n ' : ' '}domain("${style.location.join(`"),\n domain("`)}") {`;
for (let rule of style.rules) {
let style = `\n ${rule.style.replace(/;\s/g, ';\n ')}`;
let selectors = [];
if (!(rule.selector instanceof Array)) {
selectors.push(rule.selector);
} else {
rule.selector.sort(strcomp);
let pos, next, str = rule.selector.join(',');
let lim = 75;
while (str.length > lim && str.includes(',')) {
pos = str.indexOf(',');
next = str.indexOf(',', pos + 1);
while (next > 0 && next < lim) {
pos = next;
next = str.indexOf(',', pos + 1);
}
selectors.push(str.slice(0, pos));
str = str.slice(pos + 1);
}
selectors.push(str);
}
let joinedSelectors = `\n ${selectors.join(',\n ')}`.replace(/,/g, ', ');
css += `${joinedSelectors} {${style}\n }`;
}
css += '\n}\n';
}
template = template.replace('%css%', css);
}
if (template.includes('%css%'))
throw 'Something went wrong with CSS generation!';
fs.writeFileSync(fixesFileName, template, fileEncoding);
console.log('Done.');
})();