-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbackup-search-engines.js
186 lines (178 loc) · 6 KB
/
backup-search-engines.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
// Backup Search Engines
// version 2022.4.0
// https://forum.vivaldi.net/post/277594
// Adds functionality to backup and restore search engines in
// vivaldi://settings/search.
(function backupSearchEngines() {
function msg(print) {
clearTimeout(msgTimeout);
if (print === "backup") {
info.innerText = "Backup copied to clipboard";
} else if (print === "restore") {
info.innerText = "Search engines restored";
} else {
info.innerText = "Code error, aborted";
}
msgTimeout = setTimeout(() => (info.innerText = ""), 5000);
}
function bringingItAllBackHome(remains, defaultsArray) {
vivaldi.searchEngines.getTemplateUrls((engines) => {
const getKeys = engines.templateUrls.map((e) => e.keyword);
for (let i = 0; i < defaultsArray.length; i++) {
const index = getKeys.lastIndexOf(defaultsArray[i][0]);
const id = engines.templateUrls[index].id.toString();
const ds = defaultsArray[i][1];
vivaldi.searchEngines.setDefault(ds, id);
}
remains.forEach((remove) => {
vivaldi.searchEngines.removeTemplateUrl(remove);
});
msg("restore");
});
}
function exec(collection) {
vivaldi.searchEngines.getTemplateUrls((engines) => {
const oldDefaults = [
engines.defaultImage,
engines.defaultPrivate,
engines.defaultSearch,
];
const newDefaults = [
collection.defaultImage,
collection.defaultPrivate,
collection.defaultSearch,
collection.defaultSearchField,
collection.defaultSearchFieldPrivate,
collection.defaultSpeeddials,
collection.defaultSpeeddialsPrivate,
];
engines.templateUrls.forEach((engine) => {
if (oldDefaults.indexOf(engine.id) === -1) {
vivaldi.searchEngines.removeTemplateUrl(engine.id);
}
});
console.info("restoring search engines...");
const defaultsArray = [];
collection.templateUrls.forEach((collect) => {
vivaldi.searchEngines.addTemplateUrl(collect, () => {
console.info(` \u2022 ${collect.name}`);
if (newDefaults.indexOf(collect.id) > -1) {
const indeces = newDefaults
.map((e, i) => (e === collect.id ? i : ""))
.filter(String);
indeces.forEach((index) => {
let ds;
if (index === 0) {
ds = vivaldi.searchEngines.DefaultType.DEFAULT_IMAGE;
} else if (index === 1) {
ds = vivaldi.searchEngines.DefaultType.DEFAULT_PRIVATE;
} else if (index === 2) {
ds = vivaldi.searchEngines.DefaultType.DEFAULT_SEARCH;
} else if (index === 3) {
ds = vivaldi.searchEngines.DefaultType.DEFAULT_SEARCH_FIELD;
} else if (index === 4) {
ds = vivaldi.searchEngines.DefaultType.DEFAULT_SEARCH_FIELD_PRIVATE;
} else if (index === 5) {
ds = vivaldi.searchEngines.DefaultType.DEFAULT_SPEEDDIALS;
} else {
ds = vivaldi.searchEngines.DefaultType.DEFAULT_SPEEDDIALS_PRIVATE;
}
const tunnel = [collect.keyword, ds];
defaultsArray.push(tunnel);
});
}
});
});
const remains = [...new Set(oldDefaults)];
bringingItAllBackHome(remains, defaultsArray);
});
}
function restore(e) {
e.preventDefault();
e.stopPropagation();
let backupCode;
let collection;
if (e.type === "paste") {
const clipboardData = e.clipboardData;
backupCode = clipboardData.getData("text");
} else {
backupCode = e.dataTransfer.getData("text");
}
try {
collection = JSON.parse(backupCode);
} catch (err) {
msg("error");
return;
}
if (
"defaultImage" in collection &&
"defaultPrivate" in collection &&
"defaultSearch" in collection
) {
exec(collection);
} else {
msg("error");
}
}
function backup() {
vivaldi.searchEngines.getTemplateUrls((engines) => {
const backupCode = JSON.stringify(engines, null, 2);
navigator.clipboard.writeText(backupCode);
msg("backup");
});
}
function ui() {
const check = document.getElementById("vm-bse-backup");
if (!check) {
const place = document.querySelector(
".setting-section > div > .setting-group.unlimited > .setting-single"
);
const btn = document.createElement("input");
btn.setAttribute("type", "button");
btn.setAttribute("value", "Backup");
btn.classList.add("vm-bse-backup");
place.insertBefore(btn, place.lastChild);
btn.addEventListener("click", backup);
const input = document.createElement("input");
input.setAttribute("type", "text");
input.setAttribute("placeholder", "Restore Backup");
input.classList.add("vm-bse-restore");
place.insertBefore(input, place.lastChild);
input.addEventListener("paste", restore);
input.addEventListener("drop", restore);
info = document.createElement("span");
info.classList.add("vm-bse-msg");
place.insertBefore(info, place.lastChild);
}
}
const css = `
.vm-bse-restore {
width: 130px;
margin-left: 6px;
margin-top: 6px;
}
.vm-bse-restore::-webkit-input-placeholder {
opacity: 1;
color: var(--colorHighlightBg);
text-align: center;
}
.vm-bse-msg {
margin-left: 12px;
}
`;
let msgTimeout;
const settingsUrl =
"chrome-extension://mpognobbkildjkofajifpdfhcoklimli/components/settings/settings.html?path=";
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.url === `${settingsUrl}search`) {
setTimeout(ui, 100);
const check = document.getElementById("vm-bse-css");
if (!check) {
const style = document.createElement("style");
style.id = "vm-bse-css";
style.innerHTML = css;
document.getElementsByTagName("head")[0].appendChild(style);
}
}
});
})();