Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: chrome extension support #72

Merged
merged 6 commits into from
Apr 2, 2024
Merged

Conversation

Kodylow
Copy link
Contributor

@Kodylow Kodylow commented Apr 1, 2024

image

loads webimint as a chrome extension

post_build.sh Outdated Show resolved Hide resolved
post_build.sh Outdated Show resolved Hide resolved
@Kodylow Kodylow force-pushed the chrome-extension branch from fb14245 to 6da5287 Compare April 2, 2024 02:06
@Kodylow Kodylow force-pushed the chrome-extension branch from 6da5287 to df3307b Compare April 2, 2024 02:08
@Kodylow
Copy link
Contributor Author

Kodylow commented Apr 2, 2024

When you run trunk serve it puts the inline scripts across multiple lines so added conditional to handle that, the sed stuff only works on trunk build when it minifies the html to 1 line

Copy link
Owner

@elsirion elsirion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll so regret merging this monstrosity of a bash script 😆

Comment on lines +63 to +122
if [ -n "${scriptContent}" ]; then
echo "${scriptContent}" >./dist/.stage/initWebimint.js
echo "Replacing original script tag in index.html with reference to initWebimint.js"
sed -i 's|<script type=module>[^<]*</script>|<script type="module" src="/initWebimint.js"></script>|' "${indexHtmlFile}"
else # using trunk serve, multiline script tags, have to extract line by line
echo "Using trunk serve, multiline script tags, have to extract line by line"
echo "Extracting script content from index.html and creating separate .js files"
# Directory where the new JS files will be stored
JS_DIR="./dist/.stage/js"
mkdir -p "$JS_DIR"
# Counter to name the extracted JS files uniquely
COUNTER=1
# Temporary file to hold the modified HTML content
TMP_HTML=$(mktemp)
# Initialize SCRIPT_OPEN to 0 before the loop
SCRIPT_OPEN=0
# Read the index.html file line by line
while IFS= read -r line || [[ -n "$line" ]]; do
if [[ $line =~ \<script.*\>\</script\> ]]; then
# Inline script tag with no content, just copy the line
echo "$line" >>"$TMP_HTML"
elif [[ $line =~ \<script.*\>(.*) ]]; then
# Opening script tag with potential inline content
SCRIPT_OPEN=1
# Capture any content on the same line as the opening script tag
SCRIPT_CONTENT="${BASH_REMATCH[1]}"
if [[ $SCRIPT_CONTENT ]]; then
# If there's inline content right after the script tag, add a newline to start accumulating correctly
SCRIPT_CONTENT+=$'\n'
fi
elif [[ $line =~ \</script\> ]]; then
# Closing script tag, write content to a new JS file
SCRIPT_FILE="$JS_DIR/extracted_$COUNTER.js"
echo "$SCRIPT_CONTENT" >"$SCRIPT_FILE"
# Replace the script tag in HTML with a reference to the new JS file
echo "<script type=\"module\" src=\"/js/extracted_$COUNTER.js\"></script>" >>"$TMP_HTML"
COUNTER=$((COUNTER + 1))
SCRIPT_OPEN=0
SCRIPT_CONTENT="" # Reset SCRIPT_CONTENT for the next script
elif [[ $SCRIPT_OPEN -eq 1 ]]; then
# Inside a script tag, accumulate the content
SCRIPT_CONTENT+="$line"$'\n'
else
# Outside script tags, just copy the line
echo "$line" >>"$TMP_HTML"
fi
done <"${indexHtmlFile}"
# Replace the original HTML file with the modified one
mv "$TMP_HTML" "${indexHtmlFile}"
# Clean up
rm -f "$TMP_HTML"
# Replace placeholder trunk address in JavaScript files
echo "Replacing {{__TRUNK_ADDRESS__}} placeholder in extracted JavaScript files"
jsFiles=$(find ./dist/.stage/js -iname "*.js")
TRUNK_ADDRESS=127.0.0.1:8080
for file in $jsFiles; do
sed -i "s/{{__TRUNK_ADDRESS__}}/${TRUNK_ADDRESS}/g" "$file"
echo "Replaced in: $file"
done
fi
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I even want to know if this was written by GPT? 😆 What a glorious bash script!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup haha, but it works

Copy link
Owner

@elsirion elsirion Apr 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Let's have a LLM at junior dev level at best, that sometimes hallucinates, code in a write-only language, what should possibly go wrong" 🤣 (I'm no stranger to this though https://github.com/elsirion/fedimint-observer/blob/181aa61e3be51993bd531d9b072375f2f0113618/src/federation/query.html#L1)

@elsirion elsirion merged commit ffe326f into elsirion:master Apr 2, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants