-
Notifications
You must be signed in to change notification settings - Fork 5
/
backup-search-engines.js
114 lines (109 loc) · 3.96 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
// Backup Search Engines
// version 2021.10.0
// https://forum.vivaldi.net/topic/35443/backup-search-engines
// Adds functionality to backup and restore search engines in
// vivaldi://settings/search.
(function () {
function _msgSearch(pnt) {
clearTimeout(_msgTimeout);
if (pnt === "backup") {
_infoSearch.innerText = "Search engines backup copied to clipboard";
} else if (pnt === "restore") {
_infoSearch.innerText = "Search engines restored";
} else {
_infoSearch.innerText = "Search engines code error";
}
_msgTimeout = setTimeout(function () {
_infoSearch.innerText = "";
}, 5000);
}
function _restoreSearch(e) {
e.preventDefault();
e.stopPropagation();
if (e.type === "paste") {
var clipboardData = e.clipboardData;
var engineCode = clipboardData.getData("text");
} else {
var engineCode = e.dataTransfer.getData("text");
}
try {
var engines = JSON.parse(engineCode);
} catch (err) {
_msgSearch("error");
return;
}
if (
"engines" in engines &&
"default" in engines &&
"defaultPrivate" in engines
) {
chrome.storage.local.set(
{ SEARCH_ENGINE_COLLECTION: engines },
function () {
_msgSearch("restore");
}
);
} else {
_msgSearch("error");
}
}
function _backupSearch() {
chrome.storage.local.get({ SEARCH_ENGINE_COLLECTION: "" }, function (back) {
const collection = back.SEARCH_ENGINE_COLLECTION;
collection.engines = collection.engines.filter(
(engine) => engine.removed === false
);
for (let i = 0; i < collection.engines.length; i++) {
collection.engines[i].historyId = i + 1;
}
collection.nextHistoryId = collection.engines.length + 1;
console.log(collection);
const engineCode = JSON.stringify(collection);
navigator.clipboard.writeText(engineCode);
_msgSearch("backup");
});
}
function searchEngines() {
const styleCheck = document.getElementById("searchEngines");
if (!styleCheck) {
const style = document.createElement("style");
style.id = "searchEngines";
style.innerHTML =
"#backupSearch, #restoreSearch {margin-left: 6px;}#restoreSearch{width: 130px;margin-top: 6px;}#restoreSearch::-webkit-input-placeholder {opacity: 1;color: var(--colorHighlightBg);text-align: center;}#msgConfirm{margin-left: 12px}";
document.getElementsByTagName("head")[0].appendChild(style);
}
const modCheck = document.getElementById("backupSearch");
if (!modCheck) {
const place = document.querySelector(
".setting-section > div > .setting-group.unlimited > .setting-single"
);
const backupBtn = document.createElement("input");
backupBtn.setAttribute("type", "button");
backupBtn.setAttribute("value", "Backup");
backupBtn.id = "backupSearch";
place.insertBefore(backupBtn, place.lastChild);
const restoreInput = document.createElement("input");
restoreInput.setAttribute("type", "text");
restoreInput.setAttribute("placeholder", "Restore Backup");
restoreInput.id = "restoreSearch";
place.insertBefore(restoreInput, place.lastChild);
_infoSearch = document.createElement("span");
_infoSearch.id = "msgConfirm";
place.insertBefore(_infoSearch, place.lastChild);
document
.getElementById("backupSearch")
.addEventListener("click", _backupSearch);
const restoreSearch = document.getElementById("restoreSearch");
restoreSearch.addEventListener("paste", _restoreSearch);
restoreSearch.addEventListener("drop", _restoreSearch);
_msgTimeout = {};
}
}
const settingsUrl =
"chrome-extension://mpognobbkildjkofajifpdfhcoklimli/components/settings/settings.html?path=";
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (changeInfo.url === `${settingsUrl}search`) {
setTimeout(searchEngines, 100);
}
});
})();