-
Notifications
You must be signed in to change notification settings - Fork 31
/
popup.js
1463 lines (1339 loc) · 59 KB
/
popup.js
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
window.onload = function statup()
{
// Enable to clear the storage.
// chrome.storage.sync.clear();
// chrome.storage.local.clear();
// localStorage.clear()
// Create the navigation.
new Navigation();
}
// ---------------------------- General functions ----------------------------
// Inserts code into the current site.
function insert(todo, code, position, mode, filename)
// todo should be > changeHTML, changeCSS or changeJS
{
chrome.tabs.query({active:true, currentWindow:true}, function(tabs)
{
chrome.tabs.sendMessage(tabs[0].id, {todo: todo, code: code, position:position, mode:mode, filename:filename});
});
}
// shows msg on the screen
function show_message(msg)
{
let message_text = document.getElementById("message-text");
message_text.textContent = msg;
message_text.style.opacity = 1;
window.setTimeout(function(){ message_text.style.opacity = 0;}, 1000);
}
// Returns the filetype of the specified filename in the "FILEEXTENSION" format.
function filename_to_kind(filename)
{
return (filename.substring(filename.lastIndexOf(".") + 1, filename.length)).toUpperCase();
}
// Converts name of the language based on the file extension.
function kind_to_language(kind)
{
if(kind === "JS"){
return "JavaScript";
}
else{
return kind;
}
}
// Get string lenght of a javascript object.
function get_file_size(file_object)
{
let stringification = JSON.stringify(file_object);
return stringification.length;
}
// Function that checks if communication between the the front and backend code is possible.
// It will run the onFail function if communication is not possible.
// This function will be running recursively until communication is possible.
function communication_test(onFail, onSuccess, retryTimeMiliSeconds=500)
{
chrome.tabs.query({active:true, currentWindow:true}, function(tabs)
{
chrome.tabs.sendMessage(tabs[0].id, {todo:"comTest"}, function(response) {
// The extension is opened on a page that it can't manipulate.
if(chrome.runtime.lastError && chrome.runtime.lastError.message === "Could not establish connection. Receiving end does not exist.") {
onFail();
setTimeout(function(){communication_test(onFail, onSuccess, retryTimeMiliSeconds)}, retryTimeMiliSeconds);
}
else{
onSuccess();
}
});
});
}
// ------------------------------------------------------------------------
// Class responsible for controlling the editor.
class Editor
{
constructor(navigator)
{
// Array that contains [filename, editor session object] combinations.
this.files = [];
this.max_open_files = 6;
this.active_file = "none";
this.previous_file = "none";
this.navigator = navigator;
this.max_synced_filesize_chars = 8000;
this.editor = ace.edit("editor");
this.editor.getSession().setMode("ace/mode/javascript");
this.editor.setTheme("ace/theme/terminal");
this.EditSession = require("ace/edit_session").EditSession;
this.editor_element = document.getElementById("editor");
this.editor_element.style.display = "none";
}
// Opens a new editor window if the file doesn't exist yet.
// and opens the file if it aleady exists.
open_file(filename, text)
{
let all_open_filenames = [];
for(let element of this.files)
{
all_open_filenames.push(element[0]);
}
// check if the file isn't already open.
if(all_open_filenames.includes(filename))
{
this.activate_file_by_name(filename);
}
else
{
// Check if the maximum amount of open files isn't already reached.
if(this.files.length < this.max_open_files)
{
this.create_window(filename, text);
this.create_file_button(filename);
this.activate_file_by_name(filename);
}
else
{
alert("Close some files first.");
}
}
}
// Opens the editor window of the file with the specified filename.
activate_file_by_name(filename)
{
this.active_file = filename;
for(let element of this.files)
{
if(element[0] === filename)
{
let index = this.navigator.get_nav_item_index_by_filename(filename);
this.navigator.nav_items[index].open = true;
this.editor.setSession(element[1]);
this.editor_element.style.display = "block";
// Focus on the textarea after opening a file.
document.getElementsByClassName("ace_text-input")[0].focus();
break;
}
}
// Make the button that corresponds with the current file active.
this.make_button_active(filename);
}
// Make the active window button look darker to make it the obvious open file.
make_button_active(filename)
{
let all_html_elements = document.getElementsByClassName("file-title-button");
let all_close_html_elements = document.getElementsByClassName("close-button");
Array.from(all_html_elements).forEach(function(element)
{
// Highlight the right button.
if(element.value === filename)
{
element.style.opacity = 1;
all_close_html_elements[Array.from(all_html_elements).indexOf(element)].style.opacity = 1;
}
// Make the other html_elements greyed out.
else
{
element.style.opacity = 0.5;
all_close_html_elements[Array.from(all_html_elements).indexOf(element)].style.opacity = 0.5;
}
});
// Resize all open file html_elements to fit the window.
this.resize_open_file_html_elements();
}
// Changes the width of the open file buttons to fit the extension window.
resize_open_file_html_elements()
{
let all_html_elements = document.getElementsByClassName("file-title-button");
let editor_width_str = this.editor_element.style["min-width"];
let button_width = Math.round((parseInt(editor_width_str.substring(0, editor_width_str.length - 2)) - 20)/all_html_elements.length);
// Also take the with of the close html_elements into a count.
button_width = button_width - 3*all_html_elements.length;
Array.from(all_html_elements).forEach(function(element)
{
element.style.width = button_width + "px";
});
}
// Hides the editor and the open files.
hide()
{
// Hide the editor element.
this.editor_element.style.display = "none";
// Hide all open files. This basically closes all the files without saving.
// So after reopening the extension, the files will be open again.
let all_open_files = document.getElementsByClassName("open-files-list-item");
//close all the files.
Array.from(this.navigator.nav_items).forEach(function(element){
element.open = false;
});
//Delete the file html_elements.
Array.from(all_open_files).forEach(function(element){
element.parentNode.removeChild(element);
});
// Clear the files list.
this.files = [];
}
// Closes the editor of the file with the specified filename.
close_file(filename)
{
let all_open_files = document.getElementsByClassName("open-files-list-item");
// Set the file to being closed
let index = this.navigator.get_nav_item_index_by_filename(filename);
this.navigator.nav_items[index].open = false;
this.save_file_by_name(filename);
// Delete the file button.
for(const element of Array.from(all_open_files))
{
if(element.childNodes[0].value === filename)
{
element.parentNode.removeChild(element);
break;
}
}
// Remove the file from this.files.
for(let i = 0; i<this.files.length; i++)
{
if(this.files[i][0] === filename)
{
this.files.splice(i, 1);
break;
}
}
// If you close the last open file, the editmenu will go away and be replaced with the main menu.
if(this.files.length === 0)
{
this.editor_element.style.display = "none";
this.navigator.disable_all_menus();
this.navigator.enable_menu_of_kind("MAIN");
}
else
{
let file_to_be_saved_after_closing = this.files[0][0];
// Open the currently open file if the closed file is not the open file.
if(this.active_file != filename)
{
file_to_be_saved_after_closing = this.active_file;
}
// Open an other file thats open.
this.activate_file_by_name(file_to_be_saved_after_closing);
this.navigator.disable_all_menus();
this.navigator.enable_menu_of_kind("EDITOR");
this.save_current_file();
}
}
// Creates a file button for a file with the specified name.
create_file_button(filename)
{
let ul = document.getElementById("open-files-list");
let li = document.createElement("li");
let file_button = document.createElement("input");
let close_button = document.createElement("a");
li.className = "open-files-list-item";
close_button.innerText = "x";
close_button.className = "close-button"
close_button.onclick = function()
{
this.close_file(filename);
}.bind(this);
file_button.type ="submit";
file_button.className ="file-title-button";
file_button.value =filename;
file_button.onclick = function()
{
// First save the old file.
this.save_current_file();
this.activate_file_by_name(filename);
this.navigator.enable_menu_of_kind("EDITOR");
// Save the new file.
this.save_current_file();
}.bind(this);
li.appendChild(file_button);
li.appendChild(close_button);
ul.appendChild(li);
this.make_button_active(filename);
return li;
}
// Creates an editor window for the file with the specified name and text.
create_window(filename, text)
{
let new_session = new this.EditSession(text);
// Disable the info text on the left of the editor if it is an HTML file.
if(filename_to_kind(filename) === "HTML")
{
new_session.setUseWorker(false);
}
// Set the editor to the correct language.
if(filename.endsWith(".js"))
{
new_session.setMode("ace/mode/javascript");
}
else if(filename.endsWith(".css"))
{
new_session.setMode("ace/mode/css");
}
else if(filename.endsWith(".html"))
{
new_session.setMode("ace/mode/html");
}
// Bind the save_current_file function to changes in the editor.
new_session.on("change", function(delta) {
this.save_current_file();
if(this.navigator.current_menu != "EDITOR")
{
this.navigator.enable_menu_of_kind("EDITOR");
}
}.bind(this));
this.files.push([filename, new_session]);
}
// Returns the text of the current edit menu.
get_current_text()
{
let current_text = this.editor.session.getValue();
return current_text;
}
// Returns the the filetype in "FILEEXTENSION" format.
// "JS" for javascript.
// "CSS" for Cascading Style Sheets.
// "HTML" for HyperText Markup Language.
get_current_filetype()
{
if(this.active_file.endsWith(".js")){
return "JS";
}
else if(this.active_file.endsWith(".css")){
return "CSS";
}
else if(this.active_file.endsWith(".html")){
return "HTML";
}
}
// Deletes the currently active file from storage.
delete_current_file()
{
let filename = this.active_file;
this.close_file(filename);
chrome.storage.sync.remove(filename, function(){});
chrome.storage.local.remove(filename, function(){});
}
// Change the filename of the currently active file.
change_current_file_name(new_filename)
{
let old_filename = this.active_file;
// Change the filename in the editor.
for(const [i, file] of this.files.entries())
{
if(file[0] === old_filename)
{
this.files[i][0] = new_filename;
this.active_file = new_filename;
break;
}
}
// Change the filename in the navigator.
for(let i = 0; i<this.navigator.nav_items.length; i++)
{
if(this.navigator.nav_items[i].filename === old_filename)
{
this.navigator.nav_items[i].filename = new_filename;
break;
}
}
let all_html_elements = document.getElementsByClassName("file-title-button");
// Put the new filebutton in the position of the old one and remove the old one.
for(const element of Array.from(all_html_elements))
{
if(element.value === old_filename)
{
// Create new filebutton.
let new_file_button = this.create_file_button(new_filename);
element.parentNode.parentNode.insertBefore(new_file_button, element.parentNode);
element.parentNode.parentNode.removeChild(element.parentNode);
break;
}
}
// Save the file in storage with a new name.
this.save_file_by_name(new_filename);
// Remove the old file from storage.
chrome.storage.sync.remove(old_filename, function(){});
chrome.storage.local.remove(old_filename, function(){});
this.resize_open_file_html_elements();
}
// Save a file to storage using its name.
save_file_by_name(filename)
{
let all_new_data = {};
let current_file_data = {};
current_file_data.filename = filename;
for(let element of this.files)
{
if(element[0] === filename)
{
let current_text = element[1].getValue();
current_file_data.text = current_text;
break;
}
}
let index = this.navigator.get_nav_item_index_by_filename(filename);
current_file_data.active_websites = this.navigator.nav_items[index].active_websites;
current_file_data.position = this.navigator.nav_items[index].position;
current_file_data.mode = this.navigator.nav_items[index].mode;
current_file_data.active = this.navigator.nav_items[index].active;
current_file_data.reload_on_remove = (this.navigator.nav_items[index].reload_on_remove === undefined) ? false : this.navigator.nav_items[index].reload_on_remove;
current_file_data.open = this.navigator.nav_items[index].open;
current_file_data.last = (this.active_file === filename);
all_new_data[filename] = current_file_data;
// Update the synced storage if the file size is small enough.
// Update the local storage otherwise.
if (get_file_size(all_new_data) >= this.max_synced_filesize_chars)
{
chrome.storage.sync.remove(filename, function(){});
chrome.storage.local.set(all_new_data, function() {
show_message("Saved!");
});
}
else
{
chrome.storage.local.remove(filename, function(){});
chrome.storage.sync.set(all_new_data, function() {
show_message("Saved!");
});
}
}
// Saves the current file and it's properties to storage.
save_current_file()
{
let current_file_name = this.active_file;
let current_text = this.get_current_text();
let checked = this.navigator.active_checkbox.checked;
let reload_on_remove = this.navigator.reload_on_remove_checkbox.checked;
let active_websites = this.navigator.enabled_sites_text_area.value;
let position = this.navigator.position_selection.options[this.navigator.position_selection.selectedIndex].value;
let mode = this.navigator.mode_selection.options[this.navigator.mode_selection.selectedIndex].value;
let all_new_data = {};
let current_file_data = {};
current_file_data.filename =current_file_name;
current_file_data.text = current_text;
current_file_data.active_websites = active_websites;
current_file_data.position = position;
current_file_data.mode = mode;
current_file_data.active = checked;
current_file_data.reload_on_remove = (reload_on_remove === undefined) ? false : reload_on_remove;
// Checks if the file was open or not.
let index = this.navigator.get_nav_item_index_by_filename(current_file_name);
current_file_data.open = this.navigator.nav_items[index].open;
current_file_data.last = true;
all_new_data[current_file_name] = current_file_data;
// Update the synced storage if the file size is small enough.
// Update the local storage otherwise.
if (get_file_size(all_new_data) >= this.max_synced_filesize_chars)
{
chrome.storage.sync.remove(current_file_name, function(){});
chrome.storage.local.set(all_new_data, function() {
show_message("Saved!");
});
}
else
{
chrome.storage.local.remove(current_file_name, function(){});
chrome.storage.sync.set(all_new_data, function() {
show_message("Saved!");
});
}
// Update '.last' when you switch files.
if(this.previous_file != this.active_file)
{
this.previous_file = this.active_file;
this.update_last_open_file();
}
}
// Whenever a different file is selected, the newly selected file should become the 'open' file.
// This method set the new file to the last opened file so that this file will be opened at restart.
update_last_open_file()
{
// Save all open files because '.last' can only be true on one, so the rest needs to be set to false.
for(let element of this.files)
{
this.save_file_by_name(element[0]);
}
}
}
// Object that represents a saved file.
class File
{
constructor(input, filename, text, active_websites, position, mode, active, reload_on_remove, open, last)
{
this.filename = filename;
this.text = text; // Contains the content of the file.
this.navigator = navigator; // Contains the navigation.
this.active_websites = active_websites // Contains all pages that the could should run on.
this.position = position; // Determines the position where the injection should take place.
this.mode= mode; //Determines if the script should be run only on the exact specified page (exact) or also on sub-pages (recursive).
this.element = input; // The button of the file.
this.active = active; // Determines if the file should be run when the specified pages are loaded.
this.reload_on_remove = (reload_on_remove === undefined) ? false : reload_on_remove; // Determines if the page should reload after removing the manipulation.
this.open = open; //Determines is open or not.
this.last = last; //Determines if this is the file that was last open.
this.kind = filename_to_kind(filename); // Determines the filetype.
}
}
// Controlls the navigation through the menus.
class Navigation
{
constructor()
{
// Get all the html elements.
// General elements.
this.navbar = document.getElementById("navbar");
this.back_div = document.getElementById("back-div");
// html_elements
this.back_button = document.getElementById("back-button");
this.reload_button = document.getElementById("reload-button");
this.new_button = document.getElementById("new-button");
this.js_button = document.getElementById("JS-button");
this.css_button = document.getElementById("CSS-button");
this.html_button = document.getElementById("HTML-button");
this.info_button = document.getElementById("info-button");
this.bug_report_button = document.getElementById("bug-report-button");
this.donate_button = document.getElementById("donate-button");
this.try_button = document.getElementById("try-button");
this.remove_try_button = document.getElementById("remove-try-button");
this.make_button = document.getElementById("make-button");
this.delete_button = document.getElementById("delete-button");
// Dropdowns
this.position_selection = document.getElementById("position-selection");
this.mode_selection = document.getElementById("mode-selection");
// Textfields
this.filename_textfield = document.getElementById("filename-textfield");
this.enabled_sites_text_area = document.getElementById("enabled-sites-text-area");
// Checkboxes
this.active_checkbox = document.getElementById("active-checkbox");
this.reload_on_remove_checkbox = document.getElementById("reload-on-remove-checkbox");
// Texts
this.info_text = document.getElementById("info-text");
this.error_text = document.getElementById("error-text");
// Labels
this.active_label = document.getElementById("active-label");
this.active_websites_label = document.getElementById("active-websites-label");
this.matching_pages_label = document.getElementById("matching-pages-label");
this.position_label = document.getElementById("position-label");
this.filename_input_label = document.getElementById("filename-input-label");
this.language_selection_label = document.getElementById("language-selection-label");
this.menu_title_label = document.getElementById("menu-title-label");
this.extra_info_label = document.getElementById("extra-info-label");
this.project_support_label = document.getElementById("project-support-label");
// Filename editing items before editing.
this.filename_label = document.getElementById("filename-label");
this.change_filename_not_editing_div = document.getElementById("change-filename-not-editing-div");
this.change_filename_label = document.getElementById("change-filename-label");
this.change_filename_button = document.getElementById("change-filename-button");
// Filename editing items while editing.
this.change_filename_editing_div = document.getElementById("change-filename-editing-div");
this.change_filename_textfield = document.getElementById("change-filename-textfield");
this.change_filename_button_div = document.getElementById("change-filename-button-div");
this.cancel_filename_change_button = document.getElementById("cancel-filename-change-button");
this.save_filename_change_button = document.getElementById("save-filename-change-button");
// Zoom factor elements.
this.zoom_out_button = document.getElementById("zoom-out-button");
this.zoom_percentage_label = document.getElementById("zoom-percentage-label");
this.zoom_in_button = document.getElementById("zoom-in-button");
// Dividers
this.menu_title_divider = document.getElementById("menu-title-divider");
this.main_menu_division_lines = document.getElementsByClassName("main-menu-division-line");
this.editor_menu_division_lines = document.getElementsByClassName("editor-menu-division-line");
// The navigation creates the editor.
this.editor = new Editor(this);
this.current_menu = "MAIN";
this.current_zoom_level = 0;
// Use the saved zoom level at startup if there is one.
if(localStorage["current_zoom_level"])
{
this.set_zoom_factor(parseInt(localStorage["current_zoom_level"]));
}
// Use default zoom level of 300% instead.
else
{
this.set_zoom_factor(200);
}
// Arrays that contain the items that are always present on that menu.
this.main_nav_items = [
this.js_button, this.css_button, this.html_button,
this.info_text, this.info_button, this.bug_report_button,
this.language_selection_label, this.donate_button, this.extra_info_label,
this.project_support_label].concat(Array.from(this.main_menu_division_lines));
this.editor_menu_items = [
this.back_div, this.try_button, this.active_websites_label,
this.enabled_sites_text_area, this.matching_pages_label, this.mode_selection,
this.delete_button, this.filename_label, this.change_filename_not_editing_div,
this.change_filename_editing_div].concat(Array.from(this.editor_menu_division_lines));
this.new_menu_items = [
this.make_button, this.filename_textfield, this.back_div,
this.filename_input_label, this.info_text];
// Array that contains all files present in the Navigator.
// All elements should be of type 'File'.
this.nav_items = [];
// Bind the right function to every html element.
this.bind_html_elements();
// Test if communication with the backend is possible.
// Enable the 'ERROR' menu if this isn't the case.
communication_test(
function(){this.enable_menu_of_kind("ERROR");}.bind(this), //onFail
function()
{
this.enable_menu_of_kind("MAIN");
this.load_open_files();
}.bind(this)); //onSuccess
setTimeout(function() {this.load_open_files();}.bind(this), 100);
}
// binds the correct functions to the html_elements.
// This method should only be run once from inside of the constructor.
bind_html_elements()
{
// Bind the button that changes the name of the current file.
this.change_filename_button.onclick = function()
{
this.change_filename_not_editing_div.style.display = "none";
this.change_filename_editing_div.style.display = "block";
this.change_filename_textfield.focus();
}.bind(this)
// Bind the button that cancels editing the filename.
this.cancel_filename_change_button.onclick = function()
{
this.change_filename_not_editing_div.style.display = "block";
this.change_filename_editing_div.style.display = "none";
}.bind(this)
// Bind the button that saves the new filename.
this.save_filename_change_button.onclick = function()
{
let new_filename = this.change_filename_textfield.value;
let split_parts = this.editor.active_file.split(".");
let file_extension = split_parts[split_parts.length - 1];
// Check if a file with the new filename already exists.
if(this.file_exists(new_filename))
{
alert("There already is a file with this name, Try a different one.");
}
else
{
// Add the correct file extension if there isn't one already.
if(!new_filename.endsWith("." + file_extension))
{
new_filename += '.' + file_extension;
}
// Change the filename of the current file.
this.editor.change_current_file_name(new_filename);
this.enable_menu_of_kind("EDITOR");
}
}.bind(this)
// Bind the info button on the main menu.
this.info_button.onclick = function()
{
window.open("https://github.com/Ruud14/Page-Manipulator", "_blank").focus();
}.bind(this);
// Bind the bug report button on the main menu.
this.bug_report_button.onclick = function()
{
window.open("https://github.com/Ruud14/Page-Manipulator/issues", "_blank").focus();
}.bind(this);
// Bind the 'active' checkbox.
this.active_checkbox.onchange = function(){
for(let element of this.nav_items)
{
if(element.filename === this.editor.active_file)
{
let index = this.nav_items.indexOf(element);
// Only allow the active checkbox to be checked when there are active websites specified.
if(this.nav_items[index].active_websites.replaceAll(/\s/g, "") === "" && this.active_checkbox.checked === true)
{
alert("You must first specify the active websites in the 'active websites' textarea. If you want this manipulation to be active on all webistes, put 'all' into the 'active websites' textarea.");
this.active_checkbox.checked = false;
}
else
{
this.nav_items[index].active = this.active_checkbox.checked;
this.editor.save_current_file();
}
break;
}
}
}.bind(this);
// Function for autosaving when changing the position of the injected code. (HTML only)
let position_change_function = function()
{
for(let element of this.nav_items)
{
if(element.filename === this.editor.active_file)
{
let index = this.nav_items.indexOf(element);
this.nav_items[index].position = this.position_selection.options[this.position_selection.selectedIndex].value;
this.editor.save_current_file();
break;
}
}
}.bind(this);
// Autosave when changing the position of the injected code. (HTML only)
this.position_selection.onkeyup = position_change_function;
this.position_selection.onchange = position_change_function;
// Function for autosaving when changing the mode. (Exact or Recursive)
let mode_change_function = function()
{
for(let element of this.nav_items)
{
if(element.filename === this.editor.active_file)
{
let index = this.nav_items.indexOf(element);
this.nav_items[index].mode = this.mode_selection.options[this.mode_selection.selectedIndex].value;
this.editor.save_current_file();
break;
}
}
}.bind(this);
// Autosave when changing the mode.
this.mode_selection.onkeyup = mode_change_function;
this.mode_selection.onchange = mode_change_function;
// Function for autosaving when changing the 'active sites' textarea.
let active_sites_textarea_change_function = function()
{
for(let element of this.nav_items)
{
if(element.filename === this.editor.active_file)
{
let index = this.nav_items.indexOf(element);
this.nav_items[index].active_websites = this.enabled_sites_text_area.value;
this.editor.save_current_file();
break;
}
}
}.bind(this);
// Function for autosaving when leaving the 'active sites' textarea.
let active_sites_textarea_leave_function = function()
{
for(let element of this.nav_items)
{
if(element.filename === this.editor.active_file)
{
let index = this.nav_items.indexOf(element);
this.nav_items[index].active_websites = this.enabled_sites_text_area.value;
if(this.enabled_sites_text_area.value.replaceAll(/\s/g, "") === "" && this.active_checkbox.checked === true)
{
this.nav_items[index].active = false;
this.active_checkbox.checked = false;
}
this.editor.save_current_file();
break;
}
}
}.bind(this)
// Autosave when changing the 'active sites' textarea.
this.enabled_sites_text_area.onkeyup = active_sites_textarea_change_function;
this.enabled_sites_text_area.onchange = active_sites_textarea_leave_function;
// Bind that button that navigates to the JavaScript page.
this.js_button.onclick = function()
{
this.enable_menu_of_kind("JS");
}.bind(this);
// Bind that button that navigates to the CSS page.
this.css_button.onclick = function()
{
this.enable_menu_of_kind("CSS");
}.bind(this);
// Bind that button that navigates to the HTML page.
this.html_button.onclick = function()
{
this.enable_menu_of_kind("HTML");
}.bind(this);
// Bind the button that reloads the page.
this.reload_button.onclick = function()
{
let filename = this.editor.active_file;
chrome.tabs.query({active:true, currentWindow:true}, function(tabs)
{
this.editor.save_current_file();
let kind = filename_to_kind(filename);
// Remove the manipulation first.
chrome.tabs.sendMessage(tabs[0].id, {todo:"remove" + kind, value: filename});
if(!this.active_checkbox.checked)
{
this.remove_try_button.style.display = "none";
this.try_button.value = "Manipulate";
}
// Reload the page.
chrome.tabs.update(tabs[0].id, {url: tabs[0].url});
}.bind(this));
}.bind(this);
// Bind the back button.
this.back_button.onclick = function()
{
this.disable_all_menus();
this.enable_menu_of_kind("MAIN");
}.bind(this);
// Bind the button that creates a new file.
this.new_button.onclick = function()
{
//open the editor.
this.disable_all_menus();
this.enable_menu_of_kind("NEW");
this.filename_textfield.focus();
}.bind(this);
// Bind the 'manipulate'/'update manipulation' button.
this.try_button.onclick = function()
{
//get the text of the current file and send it to insert.
let current_file_name = this.editor.active_file;
let current_text = this.editor.get_current_text();
let position = this.position_selection.options[this.position_selection.selectedIndex].value;
let todo = "change" + this.editor.get_current_filetype();
let mode = this.mode_selection.options[this.mode_selection.selectedIndex].value;
this.remove_try_button.style.display = "block";
this.try_button.value = "Update Manip.";
show_message("Page Manipulated");
insert(todo, current_text, position, mode, current_file_name);
}.bind(this);
// Bind the button that removes the manipulation from the web-page.
this.remove_try_button.onclick = function(e)
{
// Don't remove the manipulation when the reload_on_remove_checkbox is clicked.
if(e.target !== this.remove_try_button)
return;
// Change the buttons.
this.remove_try_button.style.display = "none";
this.try_button.value ="Manipulate";
// Send remove message to backend.
chrome.tabs.query({active:true, currentWindow:true}, function(tabs)
{
let kind = filename_to_kind(this.editor.active_file);
chrome.tabs.sendMessage(tabs[0].id, {todo:"remove" + kind, value: this.editor.active_file});
}.bind(this));
this.editor.save_current_file();
// Reload the page when the reload_on_remove_checkbox is checked.
for(let element of this.nav_items)
{
if(element.filename === this.editor.active_file)
{
let index = this.nav_items.indexOf(element);
if(this.nav_items[index].reload_on_remove === true)
{
this.active_checkbox.checked = false;
this.nav_items[index].active = false;
this.editor.save_current_file();
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.update(tabs[0].id, {url: tabs[0].url});
});
break;
}
break;
}
}
show_message("Removed Manipulation");
}.bind(this);
// Bind the checkbox that determines if the page should be reloaded after removing a manipulation.
this.reload_on_remove_checkbox.onchange = function(){
for(let element of this.nav_items)
{
if(element.filename === this.editor.active_file)
{
let index = this.nav_items.indexOf(element);
this.nav_items[index].reload_on_remove = this.reload_on_remove_checkbox.checked;
this.editor.save_current_file();
break;
}
}
}.bind(this);
// Bind the button that removes the current file from storage.
this.delete_button.onclick = function()
{
// Make sure the user didn't press 'delete' by accident.
if(confirm("Are you sure you want to delete " + this.editor.active_file + "?"))
{
chrome.tabs.query({active:true, currentWindow:true}, function(tabs)
{
// Remove the manipulation.
let kind = filename_to_kind(this.editor.active_file);
chrome.tabs.sendMessage(tabs[0].id, {todo:"remove" + kind, value: this.editor.active_file});
let index = this.get_nav_item_index_by_filename(this.editor.active_file);
// Remove the file from the editor.
this.editor.delete_current_file();
// Removes the file form the nav_items array.
if(this.nav_items.length === 1)
{
this.nav_items = [];
}
else{
this.nav_items.slice(index, 1);
}
// Enable the right menu after deleting.
if(this.editor.files.length >= 1)
{
this.enable_menu_of_kind("EDITOR");
}
else
{
this.enable_menu_of_kind("MAIN");
}
show_message("File Deleted!");
}.bind(this));
}
}.bind(this);
// Bind the button that confirms the creation of a new file.
this.make_button.onclick = function()
{
let filename = this.filename_textfield.value;
// Make sure there isn't already a file with the same name.
if(this.file_exists(filename))
{
alert("There already is a file with this name, Try a different one.");
}
else