Skip to content

Commit

Permalink
Partial Refactor + Typescript (#4)
Browse files Browse the repository at this point in the history
* TS ready

* Fix for wca-website and general improvements

* Fixes in WCA Forum and Website
  • Loading branch information
Nanush7 authored Oct 1, 2024
1 parent 69502a2 commit 8528699
Show file tree
Hide file tree
Showing 24 changed files with 707 additions and 417 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.3.0
1.4.0
45 changes: 20 additions & 25 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,26 @@ copy_files() {
local browser="$1";
rm -rf build;
mkdir -p build;
npx tsc
for file in "${FILES[@]}"; do
cp -r "$file" "$BUILD_DIR";
done
# TODO: Improve this.
cp "scripts/purify.min.js" "$BUILD_DIR/scripts/"

find "$BUILD_DIR" -type f -printf "%P\n" | while read -r file; do
if string_in_array "$file" "${VERSION_DISPLAY_FILES[@]}"; then
sed --sandbox -i -E -e "s/#\{\{version\}\}/$WSH_VERSION/" "$BUILD_DIR/$file";
find "$BUILD_DIR" -type f | while read -r file; do
if string_in_array "$(basename "$file")" "${VERSION_DISPLAY_FILES[@]}"; then
version=$(sed 's/\./\\./g' <<< "$WSH_VERSION");
sed -E -i "s/#\{\{version\}\}/$version/" "$file";
fi
if string_in_array "$file" "${BROWSER_DEPENDANT_FILES[@]}"; then
sed --sandbox -i -E -e 's/^const BROWSER = "(firefox|chrome)";/const BROWSER = "'"$browser"'";/' "$BUILD_DIR/$file";
if string_in_array "$(basename "$file")" "${BROWSER_DEPENDANT_FILES[@]}"; then
sed -E -i 's/^const BROWSER(:[\w\d\._]+)? = "(firefox|chrome)";/const BROWSER = "'"$browser"'";/' "$file";
fi
done
}


# Start:
if [[ "$EUID" -eq 0 ]]; then
echo "Please do not run this script as root.";
echo "Aborting..."
Expand All @@ -41,8 +47,12 @@ if [[ "$target" == "package" ]]; then
# Most of the process is the same.
target="build";
package=0;
else
elif [[ "$target" == "build" ]]; then
package=1;
else
echo "Unknown target operation.";
echo "Usage: build.sh <build|package> <firefox|chrome>";
exit 1;
fi
readonly BROWSER="$2";

Expand All @@ -55,21 +65,15 @@ readonly CHROME_MIN_VERSION="102";
readonly BACKGROUND_FILE="scripts\/background.js";

# JS files that contain a "const BROWSER = '...';" line.
readonly BROWSER_DEPENDANT_FILES=("scripts/wsh-event-injection.js" "scripts/background.js");
readonly BROWSER_DEPENDANT_FILES=("wsh-event-injection.js" "background.js");

# Files that contain a "#{{version}}" line.
readonly VERSION_DISPLAY_FILES=("html/popup.html");
readonly VERSION_DISPLAY_FILES=("popup.html");

# Files to be copied to the build directory.
readonly FILES=("css" "data" "html" "img" "scripts" "LICENSE" "README.md");
readonly FILES=("css" "data" "html" "img" "LICENSE" "README.md");
readonly BUILD_DIR="build";

if [[ -z "$target" || -z "$BROWSER" ]]; then
echo "Missing arguments";
echo "Usage: build.sh <build|develop|package> <firefox|chrome>";
exit 1;
fi

if [[ "$BROWSER" == "firefox" ]]; then
min_version='"browser_specific_settings": {"gecko": {"strict_min_version": "'$FIREFOX_MIN_VERSION'"}}';
service_worker='"scripts": ["'$BACKGROUND_FILE'"]';
Expand All @@ -84,19 +88,10 @@ fi
if [[ "$target" == "build" ]]; then
manifest_output_path="$BUILD_DIR/manifest.json";
copy_files "$BROWSER";
elif [[ "$target" == "develop" ]]; then
manifest_output_path="manifest.json";
# If the file is browser dependant, replace the BROWSER constant.
for file in "${BROWSER_DEPENDANT_FILES[@]}"; do
sed --sandbox -i -E -e 's/^const BROWSER = "(firefox|chrome)";/const BROWSER = "'"$BROWSER"'";/' "$file";
done
else
echo "Invalid operation argument: $target";
exit 1;
fi

# Replace #{{...}} with the actual values in the manifest file.
if ! sed --sandbox -E -e "s/#\{\{name\}\}/$WSH_NAME/" \
if ! sed -E -e "s/#\{\{name\}\}/$WSH_NAME/" \
-e "s/#\{\{description\}\}/$WSH_DESCRIPTION/" \
-e "s/#\{\{version\}\}/$WSH_VERSION/" \
-e "s/#\{\{min_version\}\}/$min_version/" \
Expand Down
2 changes: 1 addition & 1 deletion data/default-options.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"enabled": true,
"catch_links": false,
"catch-links": false,
"justify-box-text": false,
"box-font-size": 14,
"box-timeout": 15
Expand Down
2 changes: 1 addition & 1 deletion html/info.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

<p>Here you have the list of the stuff you can link (the "Text" column represents the text you need to select; case <b>insensitive</b>):</p>

<!-- Rows defined in scripts/info.js -->
<!-- Rows defined in scripts/info.ts -->
<table id="documents">
<tr>
<th>Item</th>
Expand Down
1 change: 1 addition & 0 deletions html/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="../css/popup.css" />
<title></title>
</head>
<body>
<hr class="color-line">
Expand Down
6 changes: 3 additions & 3 deletions metadata/manifest.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
"content_scripts": [
{
"matches": ["https://www.worldcubeassociation.org/*"],
"js": ["scripts/purify.min.js", "scripts/content/base.js", "scripts/content/wca-website.js", "scripts/content/main.js"]
"js": ["scripts/purify.min.js", "scripts/content/base.js", "scripts/content/wca-website.js", "scripts/factory.js", "scripts/content/main.js"]
},
{
"matches": ["https://forum.worldcubeassociation.org/*"],
"js": ["scripts/purify.min.js", "scripts/content/base.js", "scripts/content/wca-forum.js", "scripts/content/main.js"]
"js": ["scripts/purify.min.js", "scripts/content/base.js", "scripts/content/wca-forum.js", "scripts/factory.js", "scripts/content/main.js"]
},
{
"matches": ["https://mail.google.com/*"],
"js": ["scripts/purify.min.js", "scripts/content/base.js", "scripts/content/gmail.js", "scripts/content/main.js"]
"js": ["scripts/purify.min.js", "scripts/content/base.js", "scripts/content/gmail.js", "scripts/factory.js", "scripts/content/main.js"]
}
],
"background": {
Expand Down
98 changes: 95 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"devDependencies": {
"@types/chrome": "^0.0.268",
"@types/codemirror": "^5.60.15",
"@types/dompurify": "^3.0.5",
"@types/firefox-webext-browser": "^120.0.3",
"@types/tern": "^0.23.9",
"typescript": "^5.4.5"
}
}
Loading

0 comments on commit 8528699

Please sign in to comment.