-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-extension.sh
More file actions
executable file
·1355 lines (1208 loc) · 45.3 KB
/
build-extension.sh
File metadata and controls
executable file
·1355 lines (1208 loc) · 45.3 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Flutter to Chrome Extension Builder (Unified)
# Converts a Flutter web app to a Chrome extension with WASM and JavaScript support
set -e # Exit on any error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
FLUTTER_PROJECT_DIR="."
BUILD_DIR="build/web"
EXTENSION_DIR="" # Will be set as mandatory parameter
EXTENSION_NAME="Flutter App Extension"
EXTENSION_VERSION="1.0.0"
EXTENSION_DESCRIPTION="" # Will be set based on build type and mode
BUILD_TYPE="wasm" # Default to WASM, can be changed with --web option
EXTENSION_MODE="popup" # Default to popup, can be changed with --content_scripts option
DEBUG_MODE="true" # Default to debug mode, can be changed with --no-debug option
# Helper functions
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Parse command line arguments
EXTENSION_DIR_SET=false
while [[ $# -gt 0 ]]; do
case $1 in
--output)
EXTENSION_DIR="$2"
EXTENSION_DIR_SET=true
shift 2
;;
--name)
EXTENSION_NAME="$2"
shift 2
;;
--version)
EXTENSION_VERSION="$2"
shift 2
;;
--description)
EXTENSION_DESCRIPTION="$2"
shift 2
;;
--no-build)
NO_BUILD=true
shift 1
;;
--no-debug)
DEBUG_MODE="false"
shift 1
;;
--web)
BUILD_TYPE="web"
shift 1
;;
--wasm)
BUILD_TYPE="wasm"
shift 1
;;
--popup)
EXTENSION_MODE="popup"
shift 1
;;
--content_scripts)
EXTENSION_MODE="content_scripts"
shift 1
;;
--help)
echo "Flutter to Chrome Extension Builder (Unified)"
echo ""
echo "Usage: $0 --output DIRECTORY [OPTIONS]"
echo ""
echo "Required:"
echo " --output DIR Output directory for Chrome extension"
echo ""
echo "Optional:"
echo " --name NAME Extension name (default: '$EXTENSION_NAME')"
echo " --version VERSION Extension version (default: '$EXTENSION_VERSION')"
echo " --description DESC Extension description"
echo " --web Build JavaScript version (dart2js)"
echo " --wasm Build WebAssembly version (dart2wasm) [default]"
echo " --popup Generate popup mode extension [default]"
echo " --content_scripts Generate content scripts mode extension"
echo " --no-build Skip Flutter build step"
echo " --no-debug Remove console debug messages for production"
echo " --help Show this help message"
echo ""
echo "Examples:"
echo " $0 --output chrome-extension"
echo " $0 --output my-extension --name 'My Flutter Extension' --version '2.0.0'"
echo " $0 --output web-extension --web # JavaScript version"
echo " $0 --output wasm-extension --wasm # WebAssembly version"
echo " $0 --output popup-ext --popup # Popup mode"
echo " $0 --output content-ext --content_scripts # Content scripts mode"
exit 0
;;
*)
log_error "Unknown option: $1"
echo "Use --help for usage information"
exit 1
;;
esac
done
# Check if output directory was provided
if [ "$EXTENSION_DIR_SET" = false ] || [ -z "$EXTENSION_DIR" ]; then
log_error "Output directory is required!"
echo ""
echo "Usage: $0 --output DIRECTORY [OPTIONS]"
echo ""
echo "Examples:"
echo " $0 --output chrome-extension"
echo " $0 --output my-extension --name 'My Flutter Extension'"
echo ""
echo "Use --help for more information"
exit 1
fi
# Validation
if [ ! -f "pubspec.yaml" ]; then
log_error "No pubspec.yaml found. Please run this script from the Flutter project root directory."
exit 1
fi
if ! command -v flutter &> /dev/null; then
log_error "Flutter is not installed or not in PATH"
exit 1
fi
# Set description based on build type and mode if not provided
if [ -z "$EXTENSION_DESCRIPTION" ]; then
if [ "$EXTENSION_MODE" = "content_scripts" ]; then
if [ "$BUILD_TYPE" = "wasm" ]; then
EXTENSION_DESCRIPTION="A Chrome content script running Flutter with WebAssembly"
else
EXTENSION_DESCRIPTION="A Chrome content script running Flutter with JavaScript"
fi
else
if [ "$BUILD_TYPE" = "wasm" ]; then
EXTENSION_DESCRIPTION="A Chrome extension popup running Flutter with WebAssembly"
else
EXTENSION_DESCRIPTION="A Chrome extension popup running Flutter with JavaScript"
fi
fi
fi
log_info "Starting Flutter to Chrome Extension build process..."
log_info "Extension Name: $EXTENSION_NAME"
log_info "Extension Version: $EXTENSION_VERSION"
log_info "Output Directory: $EXTENSION_DIR"
log_info "Build Type: $BUILD_TYPE"
log_info "Extension Mode: $EXTENSION_MODE"
# Step 1: Build Flutter web
if [ "$NO_BUILD" = true ]; then
log_info "Step 1/6: Skipping Flutter build (--no-build flag)"
else
if [ "$BUILD_TYPE" = "wasm" ]; then
log_info "Step 1/7: Building Flutter web with WASM support..."
flutter build web --wasm --debug
else
log_info "Step 1/7: Building Flutter web with JavaScript (dart2js)..."
flutter build web --debug
fi
log_success "Flutter web build completed"
fi
# Step 2: Create extension directory
log_info "Step 2/6: Setting up Chrome extension directory..."
if [ -d "$EXTENSION_DIR" ]; then
log_warning "Extension directory exists, cleaning up..."
rm -rf "$EXTENSION_DIR"
fi
mkdir -p "$EXTENSION_DIR"
log_success "Extension directory created"
# Step 3: Copy Flutter build files
log_info "Step 3/6: Copying Flutter build files..."
cp -r "$BUILD_DIR"/* "$EXTENSION_DIR/"
log_success "Flutter files copied"
# Step 4: Create Chrome extension manifest
log_info "Step 4/6: Creating Chrome extension manifest..."
# Create base manifest
cat > "$EXTENSION_DIR/manifest.json" << EOF
{
"manifest_version": 3,
"name": "$EXTENSION_NAME",
"version": "$EXTENSION_VERSION",
"description": "$EXTENSION_DESCRIPTION",
"permissions": [
"activeTab"
],
"icons": {
"16": "icons/Icon-192.png",
"48": "icons/Icon-192.png",
"128": "icons/Icon-512.png"
},
"web_accessible_resources": [
{
"resources": [
"assets/*",
"canvaskit/*",
"canvaskit/skwasm.js",
"canvaskit/skwasm.wasm",
"canvaskit/skwasm_heavy.js",
"canvaskit/skwasm_heavy.wasm",
"canvaskit/canvaskit.js",
"canvaskit/canvaskit.wasm",
"*.js",
"*.mjs",
"*.wasm",
"flutter.js",
"flutter_bootstrap.js",
"flutter_init.js",
"flutter_service_worker.js",
"main.dart.js",
"main.dart.mjs",
"main.dart.wasm"
],
"matches": ["<all_urls>"]
}
]
}
EOF
# Add mode-specific configuration using jq or manual editing
if [ "$EXTENSION_MODE" = "popup" ]; then
# Add popup configuration
jq '.action = {"default_popup": "popup.html", "default_title": "'"$EXTENSION_NAME"'"} | .content_security_policy = {"extension_pages": "script-src '\''self'\'' '\''wasm-unsafe-eval'\''; object-src '\''self'\''; connect-src '\''self'\'' data: blob: https://fonts.gstatic.com; font-src '\''self'\'' https://fonts.gstatic.com;"}' "$EXTENSION_DIR/manifest.json" > "$EXTENSION_DIR/manifest_temp.json" && mv "$EXTENSION_DIR/manifest_temp.json" "$EXTENSION_DIR/manifest.json" 2>/dev/null || {
# Fallback if jq is not available - recreate the manifest with popup config
cat > "$EXTENSION_DIR/manifest.json" << EOF
{
"manifest_version": 3,
"name": "$EXTENSION_NAME",
"version": "$EXTENSION_VERSION",
"description": "$EXTENSION_DESCRIPTION",
"permissions": [
"activeTab"
],
"action": {
"default_popup": "popup.html",
"default_title": "$EXTENSION_NAME"
},
"icons": {
"16": "icons/Icon-192.png",
"48": "icons/Icon-192.png",
"128": "icons/Icon-512.png"
},
"web_accessible_resources": [
{
"resources": [
"assets/*",
"canvaskit/*",
"canvaskit/skwasm.js",
"canvaskit/skwasm.wasm",
"canvaskit/skwasm_heavy.js",
"canvaskit/skwasm_heavy.wasm",
"canvaskit/canvaskit.js",
"canvaskit/canvaskit.wasm",
"*.js",
"*.mjs",
"*.wasm",
"flutter.js",
"flutter_bootstrap.js",
"flutter_init.js",
"flutter_service_worker.js",
"main.dart.js",
"main.dart.mjs",
"main.dart.wasm"
],
"matches": ["<all_urls>"]
}
],
"content_security_policy": {
"extension_pages": "script-src 'self' 'wasm-unsafe-eval'; object-src 'self'; connect-src 'self' data: blob: https://fonts.gstatic.com; font-src 'self' https://fonts.gstatic.com;"
}
}
EOF
}
elif [ "$EXTENSION_MODE" = "content_scripts" ]; then
# Add content scripts configuration
jq '.content_scripts = [{"matches": ["<all_urls>"], "js": ["content_script.js"], "css": ["content_script.css"], "run_at": "document_end"}] | .permissions += ["scripting", "storage"] | .content_security_policy = {"extension_pages": "script-src '\''self'\'' '\''wasm-unsafe-eval'\''; object-src '\''self'\''; connect-src '\''self'\'' data: blob: https://fonts.gstatic.com; font-src '\''self'\'' https://fonts.gstatic.com;"}' "$EXTENSION_DIR/manifest.json" > "$EXTENSION_DIR/manifest_temp.json" && mv "$EXTENSION_DIR/manifest_temp.json" "$EXTENSION_DIR/manifest.json" 2>/dev/null || {
# Fallback if jq is not available - recreate the manifest with content scripts config
cat > "$EXTENSION_DIR/manifest.json" << EOF
{
"manifest_version": 3,
"name": "$EXTENSION_NAME",
"version": "$EXTENSION_VERSION",
"description": "$EXTENSION_DESCRIPTION",
"permissions": [
"activeTab",
"scripting",
"storage"
],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content_script.js"],
"css": ["content_script.css"],
"run_at": "document_end"
}
],
"icons": {
"16": "icons/Icon-192.png",
"48": "icons/Icon-192.png",
"128": "icons/Icon-512.png"
},
"web_accessible_resources": [
{
"resources": [
"assets/*",
"canvaskit/*",
"canvaskit/skwasm.js",
"canvaskit/skwasm.wasm",
"canvaskit/skwasm_heavy.js",
"canvaskit/skwasm_heavy.wasm",
"canvaskit/canvaskit.js",
"canvaskit/canvaskit.wasm",
"*.js",
"*.mjs",
"*.wasm",
"flutter.js",
"flutter_bootstrap.js",
"flutter_init.js",
"flutter_service_worker.js",
"main.dart.js",
"main.dart.mjs",
"main.dart.wasm"
],
"matches": ["<all_urls>"]
}
],
"content_security_policy": {
"extension_pages": "script-src 'self' 'wasm-unsafe-eval'; object-src 'self'; connect-src 'self' data: blob: https://fonts.gstatic.com; font-src 'self' https://fonts.gstatic.com;"
}
}
EOF
}
fi
log_success "Manifest created"
# Step 5: Patch Flutter files for container targeting
log_info "Step 5/7: Patching Flutter files for Chrome extension compatibility..."
# Function to patch Flutter files to use our container
patch_flutter_files() {
log_info "Patching Flutter bootstrap file..."
# Patch flutter_bootstrap.js to inject container targeting
if [ -f "$EXTENSION_DIR/flutter_bootstrap.js" ]; then
# Create backup
cp "$EXTENSION_DIR/flutter_bootstrap.js" "$EXTENSION_DIR/flutter_bootstrap.js.backup"
# Remove existing auto-initialization to prevent conflicts
sed -i '/_flutter\.loader\.load({/,/});$/d' "$EXTENSION_DIR/flutter_bootstrap.js"
# Also remove any buildConfig that conflicts
sed -i '/_flutter\.buildConfig = /d' "$EXTENSION_DIR/flutter_bootstrap.js"
# Add completely rewritten Chrome extension patch with conditional debug messages
if [ "$DEBUG_MODE" = "true" ]; then
LOG_PATCH_START="console.log('Applying Chrome extension Flutter patch...');"
LOG_EXT_ID="console.log('Extension ID detected:', extensionId);"
LOG_BUILD_CONFIG_WASM="console.log('Setting buildConfig for WASM build');"
LOG_BUILD_CONFIG_JS="console.log('Setting buildConfig for JavaScript build');"
LOG_INIT_START="console.log('Starting Flutter initialization with Chrome extension configuration...');"
LOG_WASM_LOAD="console.log('Loading WASM build with Skwasm renderer...');"
LOG_WASM_ENTRYPOINT="console.log('Flutter WASM entrypoint loaded via loader.load...');"
LOG_WASM_HOST="console.log('Using Chrome extension container as Flutter host:', container);"
LOG_WASM_HOST_ERR="console.error('Flutter container not found! Falling back to body');"
LOG_WASM_ENGINE="console.log('Flutter WASM engine initialized, running app...');"
LOG_WASM_SUCCESS="console.log('Flutter WASM app started successfully in Chrome extension container');"
LOG_WASM_ERROR="console.error('Error initializing Flutter WASM engine in Chrome extension:', error);"
LOG_JS_LOAD="console.log('Loading JavaScript build with CanvasKit renderer...');"
LOG_JS_URL="console.log('Using Chrome extension URL for entrypoint:', url);"
LOG_JS_URL_WARN="console.warn('Extension ID not available, using relative URL');"
LOG_JS_ENTRYPOINT="console.log('Flutter JS entrypoint loaded, initializing with Chrome extension container...');"
LOG_JS_HOST="console.log('Using Chrome extension container as Flutter host:', container);"
LOG_JS_HOST_ERR="console.error('Flutter container not found! Falling back to body');"
LOG_JS_ENGINE="console.log('Flutter JS engine initialized, running app...');"
LOG_JS_SUCCESS="console.log('Flutter JS app started successfully in Chrome extension container');"
LOG_JS_ERROR="console.error('Error initializing Flutter JS engine in Chrome extension:', error);"
LOG_PATCH_END="console.log('Chrome extension Flutter patch applied');"
LOG_FETCH_INT="console.log('Flutter fetch intercepted:', url);"
LOG_FETCH_RED="console.log('Flutter fetch redirected:', url, '->', newUrl);"
LOG_IMPORT_INT="console.log('Dynamic import intercepted:', url);"
LOG_IMPORT_RED="console.log('Dynamic import redirected:', url, '->', newUrl);"
LOG_C_FUNC="console.log('Flutter c() function redirected:', parts, '->', fullUrl);"
LOG_EXT_ID_ERR="console.error('Extension ID not detected - Flutter URLs will not work properly');"
# Content script debug messages
LOG_INIT_CONFIG="console.log('Configuring Flutter for Chrome extension environment...');"
LOG_CONTENT_CONFIG="console.log('Configuring Flutter for Chrome content script environment...');"
LOG_CONTENT_FETCH_INT="console.log('Flutter fetch intercepted:', url);"
LOG_CONTENT_FETCH_RED="console.log('Flutter fetch redirected:', url, '->', newUrl);"
LOG_CONTENT_IMPORT_INT="console.log('Dynamic import intercepted:', url);"
LOG_CONTENT_IMPORT_RED="console.log('Dynamic import redirected:', url, '->', newUrl);"
LOG_CONTENT_READY="console.log('Flutter loader ready, starting Chrome content script initialization...');"
LOG_CONTENT_DELEGATED="console.log('Flutter initialization delegated to patched bootstrap - no additional setup needed');"
LOG_CONTENT_LOADED="console.log('Flutter Extension Content Script loaded');"
LOG_OVERLAY_CREATE="console.log('Creating Flutter overlay window...');"
LOG_OVERLAY_CREATED="console.log('Flutter overlay window created');"
LOG_OVERLAY_SHOWN="console.log('Flutter overlay shown');"
LOG_OVERLAY_INIT="console.log('Starting Flutter initialization after overlay is ready...');"
LOG_OVERLAY_HIDDEN="console.log('Flutter overlay hidden');"
LOG_CONTENT_INIT="console.log('Initializing Flutter in content script...');"
LOG_CONTENT_SKIP="console.log('Flutter already initialized, skipping...');"
LOG_CONTENT_EXT_ID="console.log('Setting global extension ID for Flutter:', extensionId);"
LOG_CONTENT_INIT_SUCCESS="console.log('Flutter init script loaded successfully in content script');"
LOG_CONTENT_BOOTSTRAP_SUCCESS="console.log('Flutter bootstrap loaded successfully in content script');"
LOG_CONTENT_BOOTSTRAP_ERROR="console.error('Failed to load Flutter bootstrap script in content script');"
LOG_CONTENT_INIT_ERROR="console.error('Failed to load Flutter initialization script in content script');"
LOG_CONTENT_INITIALIZED="console.log('Flutter Extension Content Script initialized. Press Ctrl+Shift+F to toggle.');"
else
LOG_PATCH_START="//"
LOG_EXT_ID="//"
LOG_BUILD_CONFIG_WASM="//"
LOG_BUILD_CONFIG_JS="//"
LOG_INIT_START="//"
LOG_WASM_LOAD="//"
LOG_WASM_ENTRYPOINT="//"
LOG_WASM_HOST="//"
LOG_WASM_HOST_ERR="//"
LOG_WASM_ENGINE="//"
LOG_WASM_SUCCESS="//"
LOG_WASM_ERROR="//"
LOG_JS_LOAD="//"
LOG_JS_URL="//"
LOG_JS_URL_WARN="//"
LOG_JS_ENTRYPOINT="//"
LOG_JS_HOST="//"
LOG_JS_HOST_ERR="//"
LOG_JS_ENGINE="//"
LOG_JS_SUCCESS="//"
LOG_JS_ERROR="//"
LOG_PATCH_END="//"
LOG_FETCH_INT="//"
LOG_FETCH_RED="//"
LOG_IMPORT_INT="//"
LOG_IMPORT_RED="//"
LOG_C_FUNC="//"
LOG_EXT_ID_ERR="//"
# Content script debug messages (disabled)
LOG_INIT_CONFIG="//"
LOG_CONTENT_CONFIG="//"
LOG_CONTENT_FETCH_INT="//"
LOG_CONTENT_FETCH_RED="//"
LOG_CONTENT_IMPORT_INT="//"
LOG_CONTENT_IMPORT_RED="//"
LOG_CONTENT_READY="//"
LOG_CONTENT_DELEGATED="//"
LOG_CONTENT_LOADED="//"
LOG_OVERLAY_CREATE="//"
LOG_OVERLAY_CREATED="//"
LOG_OVERLAY_SHOWN="//"
LOG_OVERLAY_INIT="//"
LOG_OVERLAY_HIDDEN="//"
LOG_CONTENT_INIT="//"
LOG_CONTENT_SKIP="//"
LOG_CONTENT_EXT_ID="//"
LOG_CONTENT_INIT_SUCCESS="//"
LOG_CONTENT_BOOTSTRAP_SUCCESS="//"
LOG_CONTENT_BOOTSTRAP_ERROR="//"
LOG_CONTENT_INIT_ERROR="//"
LOG_CONTENT_INITIALIZED="//"
fi
cat >> "$EXTENSION_DIR/flutter_bootstrap.js" << FLUTTER_PATCH_EOF
// CHROME EXTENSION PATCH: Complete Flutter initialization override
$LOG_PATCH_START
// Apply URL resolution fixes BEFORE Flutter initialization
// Get extension ID for resource URLs - make it global for use in initialization
(function() {
// Safely declare or reuse existing extensionId variable
if (typeof window.extensionId === 'undefined') {
window.extensionId = '';
}
var extensionId = window.extensionId;
// Try multiple methods to get the extension ID
try {
// Method 1: From chrome.runtime (may not work in content script context)
if (typeof chrome !== 'undefined' && chrome.runtime && chrome.runtime.id) {
extensionId = chrome.runtime.id;
}
} catch (e) {}
// Method 2: From global variable set by content script
if (!extensionId && window.FLUTTER_EXTENSION_ID) {
extensionId = window.FLUTTER_EXTENSION_ID;
}
// Method 3: From script src tags (existing approach)
if (!extensionId) {
const scripts = document.querySelectorAll('script[src*="chrome-extension://"]');
if (scripts.length > 0) {
const match = scripts[0].src.match(/chrome-extension:\/\/([^/]+)/);
if (match) extensionId = match[1];
}
}
// Method 4: Extract from current script URL if possible
if (!extensionId) {
try {
const currentScript = document.currentScript;
if (currentScript && currentScript.src && currentScript.src.includes('chrome-extension://')) {
const match = currentScript.src.match(/chrome-extension:\/\/([^/]+)/);
if (match) extensionId = match[1];
}
} catch (e) {}
}
$LOG_EXT_ID
if (extensionId) {
// Override document.baseURI to help Flutter resolve relative URLs correctly
const originalBaseURI = document.baseURI;
Object.defineProperty(document, 'baseURI', {
get: function() {
return 'chrome-extension://' + extensionId + '/';
},
configurable: true
});
// Override fetch to handle Chrome extension URLs
const originalFetch = window.fetch;
window.fetch = function(url, options) {
$LOG_FETCH_INT
if (typeof url === 'string') {
// If it's a relative URL or doesn't start with chrome-extension://, convert it
if (!url.startsWith('http') && !url.startsWith('chrome-extension://') && !url.startsWith('data:') && !url.startsWith('blob:')) {
const newUrl = 'chrome-extension://' + extensionId + '/' + url.replace(/^\/+/, '');
$LOG_FETCH_RED
url = newUrl;
}
}
return originalFetch.call(this, url, options);
};
// Override dynamic imports for .mjs files
const originalImport = window.import;
if (originalImport) {
window.import = function(url) {
$LOG_IMPORT_INT
if (typeof url === 'string' && !url.startsWith('http') && !url.startsWith('chrome-extension://')) {
const newUrl = 'chrome-extension://' + extensionId + '/' + url.replace(/^\/+/, '');
$LOG_IMPORT_RED
url = newUrl;
}
return originalImport.call(this, url);
};
}
// CRITICAL: Override Flutter's internal URL resolution function 'c'
// This is defined at the very beginning of flutter_bootstrap.js
// We need to patch it to use Chrome extension URLs
if (typeof window.c === 'undefined') {
// Define our own version of the 'c' function that resolves to Chrome extension URLs
window.c = function(...parts) {
const path = parts.filter(p => !!p).map((p, i) => {
if (i === 0) return p.replace(/^\/+|\/+$/g, '');
return p.replace(/^\/+|\/+$/g, '');
}).filter(p => p.length).join('/');
const fullUrl = 'chrome-extension://' + extensionId + '/' + path;
$LOG_C_FUNC
return fullUrl;
};
}
} else {
$LOG_EXT_ID_ERR
}
// Save extensionId to window for global access
window.extensionId = extensionId;
})();
// Ensure Flutter objects exist
if (!window._flutter) {
window._flutter = {};
}
// Force Flutter to use local CanvasKit files instead of CDN
window._flutter_web_locale_use_local_canvaskit = true;
if (!window._flutter.engineInitializer) {
window._flutter.engineInitializer = {};
}
window._flutter.engineInitializer.useLocalCanvasKit = true;
window._flutter.engineInitializer.canvasKitBaseUrl = "canvaskit/";
// Comprehensive override for CanvasKit CDN loading
(function() {
// Override document.createElement to intercept script tag creation
const originalCreateElement = document.createElement;
document.createElement = function(tagName) {
const element = originalCreateElement.call(this, tagName);
if (tagName.toLowerCase() === 'script') {
const originalSetAttribute = element.setAttribute;
element.setAttribute = function(name, value) {
if (name === 'src' && typeof value === 'string' && value.includes('gstatic.com/flutter-canvaskit')) {
// Extract filename and map to local path
const urlParts = value.split('/');
const filename = urlParts[urlParts.length - 1];
let localPath = 'canvaskit/' + filename;
// Handle chromium variant
if (value.includes('/chromium/')) {
localPath = 'canvaskit/chromium/' + filename;
}
console.log('🚫 Blocking CDN CanvasKit load:', value);
console.log('✅ Redirecting to local:', localPath);
originalSetAttribute.call(this, name, localPath);
} else {
originalSetAttribute.call(this, name, value);
}
};
// Also override the src property directly
const originalSrcDescriptor = Object.getOwnPropertyDescriptor(HTMLScriptElement.prototype, 'src') ||
Object.getOwnPropertyDescriptor(Element.prototype, 'src') ||
{ set: function(value) { this.setAttribute('src', value); }, get: function() { return this.getAttribute('src'); } };
Object.defineProperty(element, 'src', {
set: function(value) {
if (typeof value === 'string' && value.includes('gstatic.com/flutter-canvaskit')) {
const urlParts = value.split('/');
const filename = urlParts[urlParts.length - 1];
let localPath = 'canvaskit/' + filename;
if (value.includes('/chromium/')) {
localPath = 'canvaskit/chromium/' + filename;
}
console.log('🚫 Blocking CDN CanvasKit load via src property:', value);
console.log('✅ Redirecting to local:', localPath);
originalSrcDescriptor.set.call(this, localPath);
} else {
originalSrcDescriptor.set.call(this, value);
}
},
get: originalSrcDescriptor.get,
configurable: true
});
}
return element;
};
})();
// Set buildConfig for the correct build type
const IS_WASM_BUILD = \$IS_WASM_BUILD; // Will be replaced during build
if (IS_WASM_BUILD) {
$LOG_BUILD_CONFIG_WASM
window._flutter.buildConfig = {
"engineRevision": "1c9c20e7c3dd48c66f400a24d48ea806b4ab312a",
"builds": [{
"compileTarget": "dart2wasm",
"renderer": "skwasm",
"mainWasmPath": "main.dart.wasm",
"jsSupportRuntimePath": "main.dart.mjs"
}]
};
} else {
$LOG_BUILD_CONFIG_JS
window._flutter.buildConfig = {
"engineRevision": "1c9c20e7c3dd48c66f400a24d48ea806b4ab312a",
"builds": [{
"compileTarget": "dart2js",
"renderer": "canvaskit",
"mainJsPath": "main.dart.js"
}]
};
}
// Initialize Flutter directly with our configuration
$LOG_INIT_START
// Load Flutter with proper container targeting
if (IS_WASM_BUILD) {
$LOG_WASM_LOAD
window._flutter.loader.load({
serviceWorkerSettings: null,
config: {
hostElement: (function() {
const container = document.getElementById('flutter-extension-container');
if (container) {
$LOG_WASM_HOST
return container;
} else {
$LOG_WASM_HOST_ERR
return document.body;
}
})(),
renderer: "skwasm",
canvasKitVariant: "auto",
useLocalCanvasKit: true,
canvasKitBaseUrl: "canvaskit/"
},
onEntrypointLoaded: async function(engineInitializer) {
$LOG_WASM_ENTRYPOINT
try {
const appRunner = await engineInitializer.initializeEngine({
hostElement: (function() {
const container = document.getElementById('flutter-extension-container');
if (container) {
$LOG_WASM_HOST
return container;
} else {
$LOG_WASM_HOST_ERR
return document.body;
}
})(),
renderer: "skwasm",
canvasKitVariant: "auto",
useLocalCanvasKit: true,
canvasKitBaseUrl: "canvaskit/"
});
$LOG_WASM_ENGINE
await appRunner.runApp();
$LOG_WASM_SUCCESS
} catch (error) {
$LOG_WASM_ERROR
}
}
});
} else {
$LOG_JS_LOAD
window._flutter.loader.loadEntrypoint({
entrypointUrl: (function() {
// Use the detected extension ID to build the URL
if (window.extensionId) {
const url = 'chrome-extension://' + window.extensionId + '/main.dart.js';
$LOG_JS_URL
return url;
} else {
$LOG_JS_URL_WARN
return 'main.dart.js';
}
})(),
onEntrypointLoaded: async function(engineInitializer) {
$LOG_JS_ENTRYPOINT
try {
const appRunner = await engineInitializer.initializeEngine({
hostElement: (function() {
const container = document.getElementById('flutter-extension-container');
if (container) {
$LOG_JS_HOST
return container;
} else {
$LOG_JS_HOST_ERR
return document.body;
}
})(),
renderer: "canvaskit",
canvasKitVariant: "auto",
useLocalCanvasKit: true,
canvasKitBaseUrl: "canvaskit/"
});
$LOG_JS_ENGINE
await appRunner.runApp();
$LOG_JS_SUCCESS
} catch (error) {
$LOG_JS_ERROR
}
}
});
}
$LOG_PATCH_END
FLUTTER_PATCH_EOF
# Replace the build type marker in the bootstrap patch
if [ "$BUILD_TYPE" = "wasm" ]; then
sed -i 's/\$IS_WASM_BUILD/true/g' "$EXTENSION_DIR/flutter_bootstrap.js"
else
sed -i 's/\$IS_WASM_BUILD/false/g' "$EXTENSION_DIR/flutter_bootstrap.js"
fi
log_success "Flutter bootstrap patched with container targeting and build type selection"
fi
# No additional patching needed - the bootstrap patch handles everything
}
# Apply patches
patch_flutter_files
# Step 6: Create Flutter initialization files
log_info "Step 6/7: Creating Flutter initialization files..."
if [ "$EXTENSION_MODE" = "popup" ]; then
# Create simple flutter_init.js based on working JavaScript version
if [ -f "$EXTENSION_DIR/flutter_bootstrap.js" ]; then
# Create minimal flutter_init.js that just loads the patched bootstrap
cat > "$EXTENSION_DIR/flutter_init.js" << FLUTTER_CONFIG_EOF
// Chrome extension configuration for Flutter
$LOG_INIT_CONFIG
// Override fetch to handle Chrome extension URLs
const originalFetch = window.fetch;
window.fetch = function(url, options) {
if (typeof url === 'string' && !url.startsWith('http') && !url.startsWith('chrome-extension://')) {
if (url.includes('main.dart.') || url.includes('canvaskit/') || url.includes('skwasm') || url.includes('.wasm') || url.includes('.mjs')) {
url = chrome.runtime.getURL(url);
}
}
return originalFetch.call(this, url, options);
};
// Load the patched Flutter bootstrap
const script = document.createElement('script');
script.src = 'flutter_bootstrap.js';
script.async = true;
document.head.appendChild(script);
FLUTTER_CONFIG_EOF
log_success "Flutter initialization configured for Chrome extension"
else
log_error "flutter_bootstrap.js not found in Flutter build output"
exit 1
fi
# Create popup.html
cat > "$EXTENSION_DIR/popup.html" << POPUP_HTML_EOF
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta content="IE=Edge" http-equiv="X-UA-Compatible">
<meta name="description" content="$EXTENSION_DESCRIPTION">
<title>$EXTENSION_NAME</title>
<style>
body {
width: 500px;
height: 600px;
min-width: 500px;
min-height: 600px;
max-width: 500px;
max-height: 600px;
margin: 0;
padding: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
overflow: hidden;
box-sizing: border-box;
}
#loading {
display: flex;
justify-content: center;
align-items: center;
width: 500px;
height: 600px;
color: #666;
font-size: 14px;
position: absolute;
top: 0;
left: 0;
}
#flutter-extension-container {
width: 500px;
height: 600px;
min-width: 500px;
min-height: 600px;
max-width: 500px;
max-height: 600px;
display: block;
position: absolute;
top: 0;
left: 0;
box-sizing: border-box;
}
/* Flutter app container - force specific dimensions */
flt-glass-pane {
width: 500px !important;
height: 600px !important;
min-width: 500px !important;
min-height: 600px !important;
max-width: 500px !important;
max-height: 600px !important;
}
/* Flutter view and renderer containers */
flt-scene-host {
width: 500px !important;
height: 600px !important;
}
/* Canvas element */
canvas {
width: 500px !important;
height: 500px !important;
}
</style>
</head>
<body>
<div id="loading">Loading Flutter app...</div>
<div id="flutter-extension-container"></div>
<script src="flutter_bootstrap.js"></script>
</body>
</html>
POPUP_HTML_EOF
log_success "Popup Flutter initialization files created"
elif [ "$EXTENSION_MODE" = "content_scripts" ]; then
# Content script implementation - inline to avoid function call issues
if [ ! -f "$EXTENSION_DIR/flutter_bootstrap.js" ]; then
log_error "flutter_bootstrap.js not found in Flutter build output"
exit 1
fi
# Create complete flutter_init.js for content script without copying from bootstrap
cat > "$EXTENSION_DIR/flutter_init.js" << CONTENT_SCRIPT_INIT_EOF
// Chrome content script configuration for Flutter
$LOG_CONTENT_CONFIG
// Get extension ID for resource URLs
let extensionId = '';
try {
extensionId = chrome.runtime.id || '';
} catch (e) {
// Fallback: get extension ID from script src
const scripts = document.querySelectorAll('script[src*="chrome-extension://"]');
if (scripts.length > 0) {
const match = scripts[0].src.match(/chrome-extension:\/\/([^/]+)/);
if (match) extensionId = match[1];
}
}
// Override document.baseURI to help Flutter resolve relative URLs correctly
const originalBaseURI = document.baseURI;
Object.defineProperty(document, 'baseURI', {
get: function() {
return extensionId ? 'chrome-extension://' + extensionId + '/' : originalBaseURI;
},
configurable: true
});
// Override fetch to handle Chrome extension URLs
const originalFetch = window.fetch;
window.fetch = function(url, options) {
$LOG_CONTENT_FETCH_INT
if (typeof url === 'string') {
// If it's a relative URL or doesn't start with chrome-extension://, convert it
if (!url.startsWith('http') && !url.startsWith('chrome-extension://') && !url.startsWith('data:') && !url.startsWith('blob:')) {
if (extensionId) {
const newUrl = 'chrome-extension://' + extensionId + '/' + url.replace(/^\/+/, '');
$LOG_CONTENT_FETCH_RED
url = newUrl;
}
}
}
return originalFetch.call(this, url, options);
};
// Override dynamic imports for .mjs files
const originalImport = window.import;
if (originalImport) {
window.import = function(url) {
$LOG_CONTENT_IMPORT_INT
if (typeof url === 'string' && !url.startsWith('http') && !url.startsWith('chrome-extension://')) {
if (extensionId) {
const newUrl = 'chrome-extension://' + extensionId + '/' + url.replace(/^\/+/, '');