-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yaml
115 lines (99 loc) · 3.86 KB
/
action.yaml
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
name: "Bookmarklet Generator"
description: "Automatically generates a JavaScript bookmarklet and updates an HTML file with the bookmarklet link."
author: "Your Name or Organization"
branding:
color: "blue"
icon: "link"
inputs:
source_js_file:
description: "Path to the source JavaScript file used for creating the bookmarklet."
required: false
default: "src/bookmarklet.js"
target_html_file:
description: "Path to the target HTML file where the bookmarklet link will be updated."
required: false
default: "index.html"
target_element_id:
description: "ID of the link element in the target HTML file."
required: false
default: "target"
commit_message:
description: "Git commit message for the updated HTML file."
required: false
default: "Update bookmarklet link"
runs:
using: "composite"
steps:
- name: Check if source file exists
run: |
if [ ! -f "${{ inputs.source_js_file }}" ]; then
echo "Source file not found: ${{ inputs.source_js_file }}"
exit 1
fi
shell: bash
- name: Check if target file exists
run: |
if [ ! -f "${{ inputs.target_html_file }}" ]; then
echo "Target HTML file not found: ${{ inputs.target_html_file }}"
exit 1
fi
shell: bash
- name: Install UglifyJS
run: |
npm install uglify-js
shell: bash
- name: Execute Node.js script to generate bookmarklet
run: |
node << 'EOF'
const fs = require('fs');
const uglifyJS = require('uglify-js');
const sourceFile = "${{ inputs.source_js_file }}";
const targetFile = "${{ inputs.target_html_file }}";
const targetElementId = "${{ inputs.target_element_id }}";
try {
// Read and prepare the JS content
const jsContent = fs.readFileSync(sourceFile, 'utf-8').trim();
let processedCode = jsContent;
if (!processedCode.startsWith('(function') && !processedCode.startsWith('(() =>')) {
processedCode = `(function(){${processedCode}})();`;
}
// Minify code
const minified = uglifyJS.minify(processedCode, {
compress: true,
mangle: true
});
if (minified.error) {
console.error("UglifyJS error:", minified.error);
process.exit(1);
}
let singleLineCode = minified.code;
// Remove any extraneous newlines
singleLineCode = singleLineCode.replace(/\r?\n/g, ' ');
// Escape characters that break HTML attributes
singleLineCode = singleLineCode
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/</g, '<')
.replace(/#/g, '%23')
.replace(/>/g, '>');
const bookmarklet = 'javascript:' + singleLineCode;
const html = fs.readFileSync(targetFile, 'utf-8');
// Replace the href attribute of the link with the generated bookmarklet
const regex = new RegExp(`(<a[^>]*id="${targetElementId}"[^>]*href=")[^"]*(")`, 'i');
const updatedHtml = html.replace(regex, `$1${bookmarklet}$2`);
fs.writeFileSync(targetFile, updatedHtml, 'utf-8');
console.log(`Bookmarklet successfully created and added to ${targetFile}.`);
} catch (error) {
console.error('Error processing files:', error);
process.exit(1);
}
EOF
shell: bash
- name: Commit and Push Changes
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add "${{ inputs.target_html_file }}"
git diff-index --quiet HEAD || git commit -m "${{ inputs.commit_message }}"
git push
shell: bash