-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
29 lines (26 loc) · 1.02 KB
/
background.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
"use strict";
function openAsText(data) {
browser.webRequest.onHeadersReceived.addListener(rewriteHeaders,
{urls: [data.linkUrl]},
['blocking', 'responseHeaders']);
var removeHeader = () => {browser.webRequest.onHeadersReceived.removeListener(rewriteHeaders)};
var creating = browser.tabs.create({url: data.linkUrl});
creating.then(removeHeader, removeHeader)
}
function rewriteHeaders(e) {
var contentType = [];
var contentDisposition = [];
for (var header of e.responseHeaders) {
if (header.name == 'Content-Type') {
contentType = header;
}
if (header.name == 'Content-Disposition') {
contentDisposition = header;
}
}
contentType.value = 'text/plain;charset=UTF-8';
contentDisposition.value = '';
return {responseHeaders: e.responseHeaders};
}
browser.contextMenus.create({id: "text-open", title: "Open as Text", contexts: ["link"]});
browser.contextMenus.onClicked.addListener(openAsText);