Skip to content

Commit fcd5e7a

Browse files
committed
Support dpaste.org Fixes #67
1 parent 55cd845 commit fcd5e7a

File tree

3 files changed

+88
-9
lines changed

3 files changed

+88
-9
lines changed

manifest.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"description": "Add a custom search engine to the list of available search engines in the search bar.",
33
"manifest_version": 2,
44
"name": "Add custom search engine",
5-
"version": "4.2",
5+
"version": "5.0",
66

77
"browser_action": {
88
"default_icon": {
@@ -36,6 +36,7 @@
3636

3737
"permissions": [
3838
"https://paste.mozilla.org/api/",
39+
"https://dpaste.org/api/",
3940
"search"
4041
],
4142

search.html

+22-4
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,29 @@ <h1>Add custom search engine</h1>
111111
</div>
112112

113113
<div class="form-group">
114-
<button type="submit" class="btn btn-primary">Add custom search engine</button>
115-
<button type="submit" class="btn btn-secondary advanced adv-hidden" id="show-preview">OpenSearch XML preview</button>
116114
<p>
117-
Due to a <a target="_blank" href="https://github.com/evilpie/add-custom-search-engine/wiki/Technical-Limitation">technical limitation with Firefox WebExtensions</a>, all data entered when creating your custom search engine is <b>uploaded to paste.mozilla.org</b>.
118-
After adding the search engine the data should be automatically removed.
115+
Due to a <a target="_blank" href="https://github.com/evilpie/add-custom-search-engine/wiki/Technical-Limitation">technical limitation with Firefox WebExtensions</a>, all data entered when creating your custom search engine is <b>uploaded to a third-party service</b>.
116+
You agree to the terms given by the service.
117+
After adding the search engine the data <i>should</i> be automatically removed.
118+
<b>The author of this extension is not responsible for the functionality of these services, and no warranty is provided.</b>
119+
You agree to these terms by selecting a service below.
120+
</p>
121+
122+
<div class="row">
123+
<div class="col-sm-5">
124+
<select id="input-service" class="form-control">
125+
<option disabled>Select upload service</option>
126+
<option>paste.mozilla.org (dead)</option>
127+
<option>dpaste.org</option>
128+
</select>
129+
</div>
130+
<div class="col-sm-7">
131+
<button type="submit" class="btn btn-primary">Add custom search engine</button>
132+
<button type="submit" class="btn btn-secondary advanced adv-hidden" id="show-preview">OpenSearch XML preview</button>
133+
</div>
134+
</div>
135+
136+
<p id="policy-link"></p>
119137
</div>
120138
</form>
121139

search.js

+64-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ const XML_TEMPLATE = `<?xml version="1.0" encoding="utf-8"?>
1313
<Url type="application/x-suggestions+json" method="get" template=""></Url>
1414
</OpenSearchDescription>`;
1515

16+
const SERVICES = [
17+
{
18+
api: "https://paste.mozilla.org/api/",
19+
policyName: "\u{201C}Mozilla Privacy Policy\u{201D}",
20+
policyUrl: "https://www.mozilla.org/en-US/privacy/",
21+
},
22+
{
23+
api: "https://dpaste.org/api/",
24+
policyName: "\u{201C}About dpaste.org (Privacy Statement)\u{201D}",
25+
policyUrl: "https://dpaste.org/about/",
26+
}
27+
];
28+
1629
// Work around broken paste.mozilla.org when using Cyrillic.
1730
function htmlEntityEncode(before) {
1831
let after = "";
@@ -108,8 +121,10 @@ document.querySelector("form").addEventListener("submit", async event => {
108121
// requires http(s) URLs. After the XML is downloaded to start the installation
109122
// process, the file should be automatically removed.
110123

124+
const service = SERVICES[document.querySelector("#input-service").selectedIndex - 1];
125+
111126
try {
112-
let response = await fetch("https://paste.mozilla.org/api/", {
127+
let response = await fetch(service.api, {
113128
method: "POST",
114129
body: new URLSearchParams({
115130
content: string,
@@ -121,13 +136,14 @@ document.querySelector("form").addEventListener("submit", async event => {
121136

122137
let json = await response.json();
123138
// We need the raw XML instead of the pretty HTML view
124-
// Mozilla's dpaste instance is misconfigured, we have to fix the URL.
125-
let url = json.url.replace("dpaste-base-url.example.org", "paste.mozilla.org") + "/raw";
139+
let url = json.url + "/raw";
140+
141+
let name = document.querySelector("#input-name").value;
126142

127143
let link = document.createElement("link");
128144
link.rel = "search";
129145
link.type = "application/opensearchdescription+xml";
130-
link.title = document.querySelector("#input-name").value;
146+
link.title = name;
131147
link.href = url;
132148

133149
// This doesn't actually seem to work. Firefox seems to cache.
@@ -140,6 +156,19 @@ document.querySelector("form").addEventListener("submit", async event => {
140156

141157
document.querySelector("#main").style.display = "none";
142158
document.querySelector("#instructions").style.display = "block";
159+
160+
// Force deletion as soon possible. In case one-time doesn't work properly.
161+
let id = -1;
162+
id = setInterval(async () => {
163+
const searchEngines = await browser.search.get();
164+
for (let engine of searchEngines) {
165+
if (engine.name === name) {
166+
fetch(json.url);
167+
fetch(json.url);
168+
clearInterval(id);
169+
}
170+
}
171+
}, 250);
143172
} catch(error) {
144173
alert(error);
145174
};
@@ -150,6 +179,7 @@ document.querySelector("#close").addEventListener("click", event => {
150179

151180
document.querySelector("#main").style.display = "block";
152181
document.querySelector("#instructions").style.display = "none";
182+
resetUploadService();
153183
})
154184

155185
document.querySelector("#show-preview").addEventListener("click", event => {
@@ -231,10 +261,40 @@ async function checkName(event) {
231261
}
232262
document.querySelector("#input-name").addEventListener("change", checkName);
233263

264+
document.querySelector("#input-service").addEventListener("change", ({currentTarget}) => {
265+
if (currentTarget.selectedIndex !== 0) {
266+
currentTarget.setCustomValidity("");
267+
currentTarget.reportValidity();
268+
269+
let service = SERVICES[currentTarget.selectedIndex - 1];
270+
271+
let policyLink = document.querySelector("#policy-link");
272+
policyLink.textContent = "";
273+
policyLink.append("See ");
274+
let a = document.createElement("a");
275+
a.href = service.policyUrl;
276+
a.textContent = service.policyName;
277+
a.target = "_blank";
278+
policyLink.append(a);
279+
} else {
280+
resetUploadService();
281+
}
282+
})
283+
284+
function resetUploadService() {
285+
let service = document.querySelector("#input-service");
286+
service.selectedIndex = 0;
287+
service.setCustomValidity("Select a service");
288+
// service.reportValidity();
289+
290+
document.querySelector("#policy-link").textContent = "";
291+
}
292+
234293
document.addEventListener("DOMContentLoaded", () => {
235294
showAdvanced();
236295
usePost();
237296
loadIcon();
297+
resetUploadService();
238298
});
239299

240300
// One-click selection for convience, because we can't directly link about:preferences

0 commit comments

Comments
 (0)