-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
138 lines (120 loc) · 5.13 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
// popup.js
document.addEventListener('DOMContentLoaded', function () {
const translateButton = document.getElementById('translateButton');
const languageSelect = document.getElementById('languageSelect');
const translationResult = document.getElementById('translationResult');
const originalSubsCheckbox = document.getElementById('originalSubs');
let isEnabled = false;
let isOriginalSubsVisible = true; // Variable to track the visibility of the original subtitles
// Retrieve the extension state and original subtitles visibility from storage when the popup is opened
chrome.storage.sync.get(['isEnabled', 'isOriginalSubsVisible'], function (result) {
isEnabled = result.isEnabled;
isOriginalSubsVisible = result.isOriginalSubsVisible;
updateUI(isEnabled); // Update the UI based on the stored extension state
originalSubsCheckbox.checked = isOriginalSubsVisible; // Set the checkbox state based on the stored visibility
// Send message to content script to update the original subtitles visibility based on the stored value
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, { action: 'toggleOriginalSubsVisibility', isOriginalSubsVisible }, function (response) {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
}
});
});
});
function toggleExtension() {
isEnabled = !isEnabled; // Toggle the isEnabled variable
if (isEnabled) {
translationResult.textContent = 'Extension enabled';
translateButton.textContent = 'Disable';
// Toggle the checkbox
originalSubsCheckbox.checked = isEnabled;
enableExtension();
} else {
translationResult.textContent = 'Extension disabled';
translateButton.textContent = 'Enable';
disableExtension();
}
}
function toggleOriginalSubsVisibility() {
isOriginalSubsVisible = originalSubsCheckbox.checked; // Update the isOriginalSubsVisible variable
// console.log(isOriginalSubsVisible)
// Send message to content script to toggle the visibility of the original subtitles
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, { action: 'toggleOriginalSubsVisibility', isOriginalSubsVisible }, function (response) {
if(isOriginalSubsVisible){
chrome.tabs.insertCSS(tabs[0].id,{ code: '.hideSub { display: block!important; }'});
}else{
chrome.tabs.insertCSS(tabs[0].id,{ code: '.hideSub { display: none!important; }'});
}
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
}
});
});
// Save the updated isOriginalSubsVisible value to storage
chrome.storage.sync.set({ isOriginalSubsVisible }, function () {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
}
});
}
function enableExtension() {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.insertCSS(tabs[0].id,{ code: '.hideSub { display: block; }'});
chrome.tabs.sendMessage(tabs[0].id, { action: 'toggleExtension', isEnabled: true }, function (response) {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
}
});
});
chrome.storage.sync.set({ isEnabled: true }, function () {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
}
});
chrome.runtime.sendMessage({ action: 'enableExtension' }, function (response) {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
}
});
}
function disableExtension() {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, { action: 'toggleExtension', isEnabled: false }, function (response) {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
}
});
});
chrome.storage.sync.set({ isEnabled: false }, function () {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
}
});
chrome.runtime.sendMessage({ action: 'disableExtension' }, function (response) {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
}
});
}
function updateUI(isEnabled) {
if (isEnabled) {
translationResult.textContent = 'Extension enabled';
translateButton.textContent = 'Disable';
originalSubsCheckbox.checked = isEnabled;
} else {
translationResult.textContent = 'Extension disabled';
translateButton.textContent = 'Enable';
}
}
translateButton.addEventListener('click', toggleExtension);
originalSubsCheckbox.addEventListener('change', toggleOriginalSubsVisibility);
languageSelect.addEventListener('change', function () {
const language = languageSelect.value;
chrome.runtime.sendMessage({ action: 'changeLanguage', language }, function (response) {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
}
});
});
});