-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathconvert.sh
76 lines (69 loc) · 3.05 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
69
70
71
72
73
74
75
76
#!/bin/bash
# Install Terser
npm install terser -g > /dev/null 2>&1
# 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 --header="User-Agent: Mozilla/5.0 (Android 14; Mobile; rv:134.0) Gecko/134.0 Firefox/134.0" --header="Content-Type: application/octet-stream" --header="Accept-Language: en-US,en;q=0.9" --header="Connection: keep-alive" --header="Upgrade-Insecure-Requests: 1" --header="Cache-Control: max-age=0" --header="Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8" --keep-session-cookies --timeout=30 "$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 and remove unnecessary locale
for file in meta/*.meta.js; do
base=$(basename "$file" .meta.js)
sed -i '/^\/\/ @name:/ { /^\/\/ @name:en/!d }' "$file"
sed -i '/^\/\/ @description/d' "$file"
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 --parse bare_returns --output js/$base.js -- $file
}
export -f compile_js
parallel -j 16 compile_js ::: pr-js/*.js
# Merge meta files and js files, check errors
for file in js/*.js; do
base=$(basename "$file" .js)
userscript_name=$(grep -m 1 -oP '^// @name:en\s+\K.*' "meta/$base.meta.js" || \
grep -m 1 -oP '^// @name\s+\K.*' "meta/$base.meta.js")
userscript_name=$(echo "$userscript_name" | tr -d '\r\n')
if [[ -z "$userscript_name" ]]; then
echo -e "\e[31mError: Missing \"$base.meta.js\"\e[0m"
continue
fi
js_file_content=$(cat "js/$base.js")
js_file_length=${#js_file_content}
if [[ $js_file_length -lt 100 ]]; then
echo -e "\e[31mFailed to convert userscript: \"$userscript_name\"\e[0m"
else
cat "meta/$base.meta.js" "js/$base.js" > "release/$base.user.js"
echo -e "Completed to convert userscript: \"$userscript_name\""
fi
done
#Releasing
mv meta/*.meta.js release/
mv List release/README.md