-
Notifications
You must be signed in to change notification settings - Fork 7
/
import-export-themes.js
297 lines (288 loc) · 10.5 KB
/
import-export-themes.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
// Theme Import and Export
// version 2021.10.0
// https://forum.vivaldi.net/topic/33154/import-and-export-themes
// Adds functionality to import, export, backup, sort and move themes to
// Vivaldi's settings page.
(function () {
function _checkImport() {
// written by tam710562
if (
typeof _test.colors !== "object" ||
typeof _test.colors.accentBg !== "string" ||
!/^#(?:[0-9a-f]{3}){1,2}$/i.test(_test.colors.accentBg) ||
typeof _test.colors.baseBg !== "string" ||
!/^#(?:[0-9a-f]{3}){1,2}$/i.test(_test.colors.baseBg) ||
typeof _test.colors.baseFg !== "string" ||
!/^#(?:[0-9a-f]{3}){1,2}$/i.test(_test.colors.baseFg) ||
typeof _test.colors.highlightBg !== "string" ||
!/^#(?:[0-9a-f]{3}){1,2}$/i.test(_test.colors.highlightBg) ||
typeof _test.name !== "string" ||
typeof _test.settings !== "object" ||
typeof _test.settings.accentFromPage !== "boolean" ||
typeof _test.settings.accentOnWindow !== "boolean" ||
(typeof _test.settings.borderRadius !== "number" &&
typeof _test.settings.borderRadius !== "string") ||
typeof _test.settings.tabsTransparent !== "boolean" ||
typeof _test.version !== "number"
) {
return false;
} else {
return true;
}
}
function _message(pnt) {
clearTimeout(_timeout);
if (pnt === "export") {
_msg.innerText = "Theme code copied to clipboard";
} else if (pnt === "backup") {
_msg.innerText = "Backup copied to clipboard";
} else if (pnt === "import") {
_msg.innerText = "Theme imported";
} else if (pnt === "restore") {
_msg.innerText = "Backup imported";
} else if (pnt === "notice") {
_msg.innerText = "Nothing to import. Check console.log";
} else if (pnt === "sort") {
_msg.innerText = "User themes sorted alphabetically";
} else {
_msg.innerText = "Theme code error";
}
_timeout = setTimeout(function () {
_msg.innerText = "";
}, 5000);
}
function _importBackup() {
chrome.storage.local.get({ THEMES_USER: "" }, function (res) {
var userThemes = res.THEMES_USER;
console.log("Importing themes...");
for (i = 0; i < _set.length; i++) {
_test = _set[i];
var test = _checkImport;
if (test()) {
var compare = userThemes.findIndex((x) => x.name == _set[i].name);
if (compare === -1) {
var ok = true;
userThemes.push(_set[i]);
console.log(_set[i].name + " imported");
} else {
console.log(_set[i].name + " is a duplicate");
}
} else {
console.log(_set[i].name + " failed");
}
}
if (ok === true) {
chrome.storage.local.set({ THEMES_USER: userThemes }, function () {
_message("restore");
});
} else {
_message("notice");
}
});
}
function _importTheme(e) {
e.stopPropagation();
e.preventDefault();
if (e.type === "paste") {
var clipboardData = e.clipboardData;
var themeCode = clipboardData.getData("text");
} else {
var themeCode = e.dataTransfer.getData("text");
}
try {
_set = JSON.parse(themeCode);
} catch (err) {
_message("error");
return;
}
if (Object.keys(_set)[0] === "colors") {
_test = _set;
var test = _checkImport;
if (test()) {
const nameField = document.querySelector(".theme-name");
nameField.select();
document.execCommand("insertText", false, _set.name);
chrome.storage.local.get({ THEMES_USER: "" }, function (imp) {
var userThemes = imp.THEMES_USER;
for (i = 0; i < userThemes.length; i++) {
if (userThemes[i].name === nameField.value) {
_set.name = nameField.value;
userThemes[i] = _set;
chrome.storage.local.set({
THEMES_USER: userThemes,
BROWSER_COLOR_ACCENT_BG: _set.colors.accentBg,
BROWSER_COLOR_BG: _set.colors.baseBg,
BROWSER_COLOR_FG: _set.colors.baseFg,
BROWSER_COLOR_HIGHLIGHT_BG: _set.colors.highlightBg,
TABCOLOR_BEHIND_TABS: _set.settings.accentOnWindow,
USE_TABCOLOR: _set.settings.accentFromPage,
BORDER_RADIUS: _set.settings.borderRadius,
USE_TAB_TRANSPARENT_TABS: _set.settings.tabsTransparent,
THEME_CURRENT: _set.name,
});
_message("import");
break;
}
}
});
} else {
_message("error");
}
} else if (Object.keys(_set)[0] === "0") {
_importBackup();
} else {
_message("error");
}
}
function _exportTheme(event) {
if (event.altKey) {
var backup = true;
}
if (event.shiftKey) {
var order = true;
}
chrome.storage.local.get(
{
THEME_CURRENT: "",
THEMES_USER: "",
},
function (exp) {
const themeName = exp.THEME_CURRENT;
const userThemes = exp.THEMES_USER;
if (backup === true) {
const themeCode = JSON.stringify(userThemes);
navigator.clipboard.writeText(themeCode);
_message("backup");
} else if (order === true) {
userThemes.sort(function (a, b) {
return a.name.localeCompare(b.name);
});
chrome.storage.local.set({ THEMES_USER: userThemes }, function () {
_message("sort");
});
} else {
for (i = 0; i < userThemes.length; i++) {
if (userThemes[i].name === themeName) {
const themeCode = JSON.stringify(userThemes[i]);
navigator.clipboard.writeText(themeCode);
_message("export");
break;
}
}
}
}
);
}
function _moveTheme() {
chrome.storage.local.get(
{
THEME_CURRENT: "",
THEMES_USER: "",
},
function (mv) {
const themeName = mv.THEME_CURRENT;
const userThemes = mv.THEMES_USER;
var index = userThemes.findIndex((x) => x.name == themeName);
if (index !== -1) {
if (_toMove === "left") {
if (index !== 0) {
var fromI = userThemes[index];
var toI = userThemes[index - 1];
userThemes[index - 1] = fromI;
userThemes[index] = toI;
} else {
return;
}
} else {
var last = userThemes.length - 1;
if (index < last) {
var fromI = userThemes[index];
var toI = userThemes[index + 1];
userThemes[index + 1] = fromI;
userThemes[index] = toI;
} else {
return;
}
}
chrome.storage.local.set({ THEMES_USER: userThemes });
}
}
);
}
function createPort() {
if (
document.querySelector(_themeBtn).classList.contains("button-pressed")
) {
const cont = document.querySelector(".theme-metadata");
const importBtn = document.createElement("input");
importBtn.setAttribute("type", "text");
importBtn.setAttribute("placeholder", "Import");
importBtn.id = "importTheme";
cont.appendChild(importBtn);
const exportBtn = document.createElement("input");
exportBtn.setAttribute("type", "submit");
exportBtn.classList.add("primary");
exportBtn.setAttribute("value", "Export");
exportBtn.setAttribute(
"title",
"Click to export theme\nAlt-click to backup all themes\nShift-click to sort themes"
);
exportBtn.id = "exportTheme";
cont.appendChild(exportBtn);
_msg = document.createElement("span");
_msg.id = "modInfo";
cont.appendChild(_msg);
document
.getElementById("exportTheme")
.addEventListener("click", _exportTheme);
const importInput = document.getElementById("importTheme");
importInput.addEventListener("paste", _importTheme);
importInput.addEventListener("drop", _importTheme);
_timeout = {};
}
}
function portThemes() {
const styleCheck = document.getElementById("portThemes");
if (!styleCheck) {
const style = document.createElement("style");
style.id = "portThemes";
style.innerHTML =
".move-left button:focus, .move-right button:focus {border-color: var(--colorBorder) !important;box-shadow: none !important;}#importTheme, #exportTheme {width: 80px;margin-left: 6px;}#importTheme::-webkit-input-placeholder {opacity: 1;color: var(--colorHighlightBg);text-align: center;}#modInfo {margin-top: 6px;margin-left: 12px;}";
document.getElementsByTagName("head")[0].appendChild(style);
}
const modCheck = document.querySelector(".move-left");
if (!modCheck) {
const group = document.createElement("div");
group.classList.add("toolbar", "toolbar-group");
group.innerHTML =
'<div class="button-toolbar move-left"><button draggable="false" tabindex="auto" title="Move Theme Left" class=""><svg width="16" height="16" viewBox="0 0 1792 1792" xmlns="http://www.w3.org/2000/svg"><path d="M1216 448v896q0 26-19 45t-45 19-45-19l-448-448q-19-19-19-45t19-45l448-448q19-19 45-19t45 19 19 45z"/></svg></button></div><hr><div class="button-toolbar move-right"><button draggable="false" tabindex="auto" title="Move Theme Right" class=""><svg width="16" height="16" viewBox="0 0 1792 1792" xmlns="http://www.w3.org/2000/svg"><path d="M1152 896q0 26-19 45l-448 448q-19 19-45 19t-45-19-19-45v-896q0-26 19-45t45-19 45 19l448 448q19 19 19 45z"/></svg></button></div>';
document
.querySelector(_themeBtn)
.parentNode.parentNode.appendChild(group);
document
.querySelector(".move-left")
.addEventListener("click", function () {
_toMove = "left";
_moveTheme();
});
document
.querySelector(".move-right")
.addEventListener("click", function () {
_toMove = "right";
_moveTheme();
});
document.querySelector(_themeBtn).addEventListener("click", function () {
setTimeout(createPort, 50);
});
}
}
const settingsUrl =
"chrome-extension://mpognobbkildjkofajifpdfhcoklimli/components/settings/settings.html?path=";
const _themeBtn =
".setting-group.unlimited > .toolbar.toolbar-default > .button-toolbar > button";
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (changeInfo.url === `${settingsUrl}themes`) {
setTimeout(portThemes, 100);
}
});
})();