-
Notifications
You must be signed in to change notification settings - Fork 7
/
convert.sh
68 lines (60 loc) · 2.16 KB
/
convert.sh
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
#!/bin/bash
# Install Terser
npm install terser -g
# Make directories
mkdir download pr-js meta js release
# Download userscripts
while read -r line; do
if [[ $line == \#* ]]; then
urls=($(echo "$line" | grep -o 'https://[^ )]*'))
if [[ ${#urls[@]} -ge 2 ]]; then
url=${urls[1]}
file=$(basename "$url")
file=$(echo "$file" | tr -cd '[:alnum:].')
if [[ -f "download/$file" ]]; then
suffix="A"
while [[ -f "download/$suffix$file" ]]; do
suffix=$(echo "$suffix" | tr "0-9A-Z" "1-9A-Z_")
done
file="$suffix$file"
fi
wget -q -U "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/119.0" "$url" -O "download/$file"
sed -i "s|$url|https://raw.githubusercontent.com/$repository/release/release/$file|g" List
fi
fi
done < List
# Split userscript to meta file and js file
for file in download/*.user.js; do
base=$(basename "$file" .user.js)
sed -n '/\/\/ ==UserScript==/,/\/\/ ==\/UserScript==/p' "$file" > "meta/$base.meta.js"
sed -n '/\/\/ ==\/UserScript==/,$p' "$file" | tail -n +2 > "pr-js/$base.js"
done
# Switch @downloadURL and @updateURL to our repository
for file in meta/*.meta.js; do
base=$(basename "$file" .meta.js)
sed -i "s|// @downloadURL .*|// @downloadURL https://raw.githubusercontent.com/$repository/release/release/$base.user.js|" "$file"
sed -i "s|// @updateURL .*|// @updateURL https://raw.githubusercontent.com/$repository/release/release/$base.meta.js|" "$file"
done
# Parallel compile js files
function compile_js() {
local file=$1
base=$(basename "$file" .js)
eval terser --compress --mangle --comments false --output js/$base.js -- $file
}
export -f compile_js
parallel -j 8 compile_js ::: pr-js/*.js
# Merge meta files and js files after compile
for file in js/*.js; do
base=$(basename "$file" .js)
cat "meta/$base.meta.js" "js/$base.js" > "release/$base.user.js"
done
#Releasing
mv meta/*.meta.js release/
mv List release/README.md
#Checking convert errors:
for meta in release/*.meta.js; do
base=${meta%.meta.js}
if [ ! -f "$base.user.js" ]; then
echo -e "\e[31m[-] Failed to convert user file $base.user.js\e[0m"
fi
done