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

Update the v4-native-client.js to replace # with ___ #42

Merged
merged 5 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions scripts/replace_hash_sign.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/bin/sh

# This script replaces the private function and property names in a file with a new name
# so that Android devices with browsers that supports old javascript syntax can run the code

# Check if filename is provided as argument
if [ $# -ne 1 ]; then
echo "Usage: $0 <filename>"
exit 1
fi

filename="$1"

# Check if the file exists
if [ ! -f "$filename" ]; then
echo "Error: File '$filename' not found."
exit 1
fi

# create an empty array to store the names
functionNames=()
propertyNames=()

# Read the file line by line and print each line
while IFS= read -r line; do
# " #name(....) {"
Copy link
Contributor

Choose a reason for hiding this comment

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

how come this pattern doesn't match the async and static variants?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It requires four leading spaces and then $name. The async and static versions are matched in the subsequent lines.

Wanted to be conservative in the matching.

if [[ $line =~ ^\ \ \ \ \#.*\{$ ]]; then
# extract the string beween # and (, and remove the rest
name=`echo "${line#*#}" | sed 's/(.*//'`
# add the name to the names array
functionNames+=("$name")
fi

# " async #name(....) {"
if [[ $line =~ ^\ \ \ \ \async\ #.*\{$ ]]; then
# extract the string beween # and (, and remove the rest
name=`echo "${line#*#}" | sed 's/(.*//'`
# add the name to the names array
functionNames+=("$name")
fi

# " static async #name(....) {"
if [[ $line =~ ^\ \ \ \ \static\ async\ #.*\{$ ]]; then
# extract the string beween # and (, and remove the rest
name=`echo "${line#*#}" | sed 's/(.*//'`
# add the name to the names array
functionNames+=("$name")
fi

if [[ $line =~ ^\ \ \ \ \static\ #.*\{$ ]]; then
# extract the string beween # and (, and remove the rest
name=`echo "${line#*#}" | sed 's/(.*//'`
# add the name to the names array
functionNames+=("$name")
fi

if [[ $line =~ ^\ \ \ \ \#.*\;$ ]]; then
# extract the string beween # and ;, and remove the rest
name=`echo "${line#*#}" | sed 's/;.*//'`
propertyNames+=("$name")
fi
done < "$filename"

# remove duplicates from the array
functionNames=($(echo "${functionNames[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
propertyNames=($(echo "${propertyNames[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))

# replace the names in the file
for name in "${functionNames[@]}"; do
echo "Replacing function name: #$name"
before="#$name("
after="___$name("
# replace the before string with the after string in the file
sed -i '' "s/$before/$after/g" $filename
done

for name in "${propertyNames[@]}"; do
echo "Replacing property name: #$name"
before="\ #$name"
after="\ ___$name"
sed -i '' "s/$before/$after/g" $filename
before="\.#$name"
after="\.___$name"
sed -i '' "s/$before/$after/g" $filename
done
3 changes: 3 additions & 0 deletions scripts/update_client_api.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@ if [ -d "$TARGET_DIR" ]; then
else
echo "Target directory $TARGET_DIR does not exist. File not copied."
fi

# Replace private functions and properties that start with #
${ORIG_DIR}/replace_hash_sign.sh "$TARGET_DIR/v4-native-client.js"
Loading
Loading