-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.js
57 lines (47 loc) · 1.93 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
function changeTheme(theme, version) {
let script;
console.log('>>>>>>>>>>>', theme);
script = `var elms = document.querySelectorAll("[data-theme='theme-swagger-by-extension']"); for(var i = 0; i < elms.length; i++){if(elms[i])elms[i].parentNode.removeChild(elms[i]);}`;
chrome.tabs.executeScript({
code: script
});
if (theme !== 'none') {
const url = chrome.extension.getURL(`themes/${version}/theme-${theme}.css`)
script = `var link = document.createElement("link"); link.href = "${url}"; link.type = "text/css"; link.rel = "stylesheet"; link.setAttribute("data-theme", "theme-swagger-by-extension"); document.getElementsByTagName("head")[0].appendChild(link);`;
}
chrome.tabs.executeScript({
code: script
});
}
function getSavedTheme(callback) {
chrome.storage.sync.get('swagger-theme', (theme) => {
chrome.storage.sync.get('swagger-version', (version) => {
callback(chrome.runtime.lastError ? null : {
'swagger-theme': theme['swagger-theme'],
'swagger-version': version['swagger-version'] || '2.x'
});
});
});
}
function saveTheme(theme, swaggerVersion) {
chrome.storage.sync.set({ 'swagger-theme': theme, 'swagger-version': swaggerVersion });
}
document.addEventListener('DOMContentLoaded', () => {
const dropdown = document.getElementById('dropdown');
const swaggerVersion = document.getElementById('swagger-version');
getSavedTheme((params) => {
if (params) {
changeTheme(params['swagger-theme'], params['swagger-version']);
dropdown.value = params['swagger-theme'];
swaggerVersion.value = params['swagger-version'];
}
});
dropdown.addEventListener('change', () => {
changeTheme(dropdown.value, swaggerVersion.value);
saveTheme(dropdown.value, swaggerVersion.value);
});
swaggerVersion.addEventListener('change', () => {
changeTheme(dropdown.value, swaggerVersion.value);
saveTheme(dropdown.value, swaggerVersion.value);
});
});