-
Notifications
You must be signed in to change notification settings - Fork 2
/
injectors.py
65 lines (51 loc) · 1.83 KB
/
injectors.py
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
import os
HEADER_BEGIN = '/* ==UserStyle=='
HEADER_END = '==/UserStyle== */'
FILENAME = './naver-dark.user.css'
def main():
with open(FILENAME, 'r') as f:
texts = f.read().splitlines()
headers = []
contents = []
is_header = False
done = False
contains_homepage = False
contains_update = False
for i in range(len(texts)):
text = texts[i]
if not done:
if text.startswith(HEADER_BEGIN):
is_header = True
continue
elif text.startswith(HEADER_END):
is_header = False
done = True
continue
if is_header:
if text.startswith('@homepageURL'):
contains_homepage = True
if text.startswith('@updateURL'):
contains_update = True
headers.append(text)
else:
contents.append(text)
print('Num of headers: {}'.format(len(headers)))
print('Num of contents: {}'.format(len(contents)))
if len(headers) == 0:
print('Invalid num of headers. Failed')
exit(1)
if len(contents) == 0:
print('Invalid num of contents. Failed')
exit(1)
if not contains_homepage:
headers.append('@homepageURL \t\thttps://github.com/DarkenPages/Naver-Dark')
if not contains_update:
headers.append('@updateURL \t\thttps://raw.githubusercontent.com/DarkenPages/Naver-Dark/master/naver-dark.user.css')
with open(FILENAME, 'w') as f:
f.write('{}\n'.format(HEADER_BEGIN))
f.write('{}\n'.format('\n'.join(headers)))
f.write('{}\n'.format(HEADER_END))
f.write('{}\n'.format('\n'.join(contents)))
print('Total {} lines of {} have been written'.format(len(headers) + len(contents), FILENAME))
if __name__ == '__main__':
main()