Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Merge pull request #34 from daviirodrig/chrome-support
Browse files Browse the repository at this point in the history
Add changes for chrome support
  • Loading branch information
54ac authored Jan 22, 2021
2 parents 3eb4568 + 77374da commit 9efcc1a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 57 deletions.
60 changes: 29 additions & 31 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ const supported = [
{ ext: ["dfxp"], ct: ["application/ttaf+xml"], type: "DFXP" }
];

const _ = browser.i18n.getMessage;
const _ = chrome.i18n.getMessage;

const manifestVersion = browser.runtime.getManifest().version;
const manifestVersion = chrome.runtime.getManifest().version;

let urlStorage = [];
let urlStorageRestore = [];
Expand Down Expand Up @@ -84,7 +84,7 @@ const addURL = requestDetails => {
queue = queue.filter(e => e !== requestDetails.requestId);

if (notifPref === false) {
browser.notifications.create("add", {
chrome.notifications.create("add", {
// id = only one notification of this type appears at a time
type: "basic",
iconUrl: "img/icon-dark-96.png",
Expand All @@ -99,14 +99,14 @@ const addURL = requestDetails => {
newEntry
) {
badgeText = urlStorage.length;
browser.browserAction.setBadgeBackgroundColor({ color: "green" });
browser.browserAction.setBadgeText({
chrome.browserAction.setBadgeBackgroundColor({ color: "green" });
chrome.browserAction.setBadgeText({
text: badgeText.toString()
});
newEntry = false;

browser.storage.local.set({ urlStorage, badgeText }).then(
() => browser.runtime.sendMessage({ urlStorage: true }) // update popup if opened
chrome.storage.local.set({ urlStorage, badgeText },
() => chrome.runtime.sendMessage({ urlStorage: true }) // update popup if opened
);
}
};
Expand All @@ -126,32 +126,31 @@ const deleteURL = message => {
);
}

browser.storage.local
.set({ urlStorage, urlStorageRestore, badgeText })
.then(() => {
browser.runtime.sendMessage({ urlStorage: true });
chrome.storage.local
.set({ urlStorage, urlStorageRestore, badgeText }, () => {
chrome.runtime.sendMessage({ urlStorage: true });
if (message.previous === false)
browser.browserAction.setBadgeText({
chrome.browserAction.setBadgeText({
text: badgeText === 0 ? "" : badgeText.toString() // only display at 1+
});
});
};

const setup = () => {
// clear everything and/or set up
browser.browserAction.setBadgeText({ text: "" });
chrome.browserAction.setBadgeText({ text: "" });

browser.storage.local.get().then(options => {
chrome.storage.local.get((options) => {
if (
(options.version &&
(options.version.split(".")[0] < manifestVersion.split(".")[0] ||
(options.version.split(".")[0] === manifestVersion.split(".")[0] &&
options.version.split(".")[1] < manifestVersion.split(".")[1]))) ||
!options.version
)
browser.storage.local.clear();
chrome.storage.local.clear();

browser.storage.local
chrome.storage.local
.set({
// first init also happens here
disablePref: options.disablePref || false,
Expand All @@ -167,15 +166,14 @@ const setup = () => {
notifPref: options.notifPref || false,
urlStorageRestore: options.urlStorageRestore || [],
version: manifestVersion
})
.then(() => {
}, () => {
if (!options.disablePref || options.disablePref === false) {
browser.webRequest.onBeforeSendHeaders.addListener(
chrome.webRequest.onBeforeSendHeaders.addListener(
urlFilter,
{ urls: ["<all_urls>"] },
["requestHeaders"]
);
browser.webRequest.onHeadersReceived.addListener(
chrome.webRequest.onHeadersReceived.addListener(
urlFilter,
{ urls: ["<all_urls>"] },
["responseHeaders"]
Expand All @@ -193,35 +191,35 @@ const setup = () => {

// restore urls on startup
if (urlStorageRestore.length > 0)
browser.storage.local.set({
chrome.storage.local.set({
urlStorageRestore,
urlStorage: []
});
});
});

browser.runtime.onMessage.addListener(message => {
chrome.runtime.onMessage.addListener(message => {
if (message.delete) deleteURL(message);
else if (message.options) {
browser.storage.local.get().then(options => {
chrome.storage.local.get((options) => {
if (
options.disablePref === true &&
browser.webRequest.onBeforeSendHeaders.hasListener(urlFilter) &&
browser.webRequest.onHeadersReceived.hasListener(urlFilter)
chrome.webRequest.onBeforeSendHeaders.hasListener(urlFilter) &&
chrome.webRequest.onHeadersReceived.hasListener(urlFilter)
) {
browser.webRequest.onBeforeSendHeaders.removeListener(urlFilter);
browser.webRequest.onHeadersReceived.removeListener(urlFilter);
chrome.webRequest.onBeforeSendHeaders.removeListener(urlFilter);
chrome.webRequest.onHeadersReceived.removeListener(urlFilter);
} else if (
options.disablePref === false &&
!browser.webRequest.onBeforeSendHeaders.hasListener(urlFilter) &&
!browser.webRequest.onHeadersReceived.hasListener(urlFilter)
!chrome.webRequest.onBeforeSendHeaders.hasListener(urlFilter) &&
!chrome.webRequest.onHeadersReceived.hasListener(urlFilter)
) {
browser.webRequest.onBeforeSendHeaders.addListener(
chrome.webRequest.onBeforeSendHeaders.addListener(
urlFilter,
{ urls: ["<all_urls>"] },
["requestHeaders"]
);
browser.webRequest.onHeadersReceived.addListener(
chrome.webRequest.onHeadersReceived.addListener(
urlFilter,
{ urls: ["<all_urls>"] },
["responseHeaders"]
Expand Down
16 changes: 8 additions & 8 deletions content/options.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

const _ = browser.i18n.getMessage; // i18n
const _ = chrome.i18n.getMessage; // i18n

const checkHeadersPref = () => {
document.getElementById("streamlinkOutput").disabled = true;
Expand Down Expand Up @@ -44,25 +44,25 @@ const checkHeadersPref = () => {
const saveOption = e => {
if (e.target.id === "copyMethod" && e.target.value !== "url") {
const prefName = "customCommand" + e.target.value;
browser.storage.local.get(prefName).then(res => {
chrome.storage.local.get(prefName, (res) => {
res[prefName]
? (document.getElementById("customCommand").value = res[prefName])
: (document.getElementById("customCommand").value = "");
});
}

if (e.target.id === "customCommand") {
browser.storage.local.set({
chrome.storage.local.set({
[e.target.id + document.getElementById("copyMethod").value]: e.target
.value
});
} else if (e.target.type === "checkbox") {
browser.storage.local.set({
chrome.storage.local.set({
[e.target.id]: e.target.checked
});
browser.runtime.sendMessage({ options: true });
chrome.runtime.sendMessage({ options: true });
} else {
browser.storage.local.set({
chrome.storage.local.set({
[e.target.id]: e.target.value
});
}
Expand All @@ -73,7 +73,7 @@ const saveOption = e => {
const restoreOptions = () => {
const options = document.getElementsByClassName("option");
// this is truly a pain
browser.storage.local.get().then(item => {
chrome.storage.local.get((item) => {
for (const option of options) {
if (option.id === "customCommand") {
const prefName =
Expand Down Expand Up @@ -121,7 +121,7 @@ document.addEventListener("DOMContentLoaded", () => {
restoreOptions();

// sync with popup changes
browser.runtime.onMessage.addListener(message => {
chrome.runtime.onMessage.addListener(message => {
if (message.options) restoreOptions();
});
});
36 changes: 18 additions & 18 deletions content/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

const defaultOptions = { tabThis: true }; // used in restoreOptions

const _ = browser.i18n.getMessage; // i18n
const _ = chrome.i18n.getMessage; // i18n

const table = document.getElementById("popupUrlList");

let urlList = [];

const copyURL = info => {
browser.storage.local.get().then(options => {
chrome.storage.local.get((options) => {
const list = { urls: [], filenames: [], methodIncomp: false };
for (const e of info) {
let code;
Expand Down Expand Up @@ -263,7 +263,7 @@ const copyURL = info => {
document.execCommand("copy");
document.body.removeChild(copyText);
if (options.notifPref !== true) {
browser.notifications.create("copy", {
chrome.notifications.create("copy", {
type: "basic",
iconUrl: "img/icon-dark-96.png",
title: _("notifCopiedTitle"),
Expand All @@ -274,7 +274,7 @@ const copyURL = info => {
});
}
} catch (e) {
browser.notifications.create("error", {
chrome.notifications.create("error", {
type: "basic",
iconUrl: "img/icon-dark-96.png",
title: _("notifErrorTitle"),
Expand All @@ -286,7 +286,7 @@ const copyURL = info => {

const deleteURL = requestDetails => {
const deleteUrlStorage = [requestDetails];
browser.runtime.sendMessage({
chrome.runtime.sendMessage({
delete: deleteUrlStorage,
previous: document.getElementById("tabPrevious").checked
}); // notify background script to update urlstorage. workaround
Expand All @@ -311,7 +311,7 @@ const clearList = () => {
idList.includes(url.requestId)
);

browser.runtime.sendMessage({
chrome.runtime.sendMessage({
delete: deleteUrlStorage,
previous: document.getElementById("tabPrevious").checked
});
Expand Down Expand Up @@ -392,7 +392,7 @@ const createList = () => {
table.appendChild(row);
};

browser.storage.local.get().then(options => {
chrome.storage.local.get((options) => {
// clear list first just in case - quick and dirty
table.innerHTML = "";

Expand All @@ -405,7 +405,7 @@ const createList = () => {
.value.toLowerCase();

// do the query first to avoid async issues
browser.tabs.query({ active: true, currentWindow: true }).then(tab => {
chrome.tabs.query({ active: true, currentWindow: true }, (tab) => {
if (document.getElementById("tabThis").checked === true) {
urlList = options.urlStorage
? options.urlStorage.filter(url => url.tabId === tab[0].id)
Expand Down Expand Up @@ -439,43 +439,43 @@ const createList = () => {
const saveOption = e => {
const options = document.getElementsByClassName("option");
if (e.target.type === "checkbox") {
browser.storage.local.set({
chrome.storage.local.set({
[e.target.id]: e.target.checked
});
browser.runtime.sendMessage({ options: true });
chrome.runtime.sendMessage({ options: true });
} else if (e.target.type === "radio") {
// update entire radio group
for (const option of options) {
if (option.name === e.target.name) {
browser.storage.local.set({
chrome.storage.local.set({
[option.id]: document.getElementById(option.id).checked
});
}
}
createList();
} else {
browser.storage.local.set({
chrome.storage.local.set({
[e.target.id]: e.target.value
});
browser.runtime.sendMessage({ options: true });
chrome.runtime.sendMessage({ options: true });
}
};

const restoreOptions = () => {
// change badge text background when clicked
browser.browserAction.setBadgeBackgroundColor({ color: "gainsboro" });
chrome.browserAction.setBadgeBackgroundColor({ color: "gainsboro" });

const options = document.getElementsByClassName("option");
// should probably consolidate this with the other one at some point
browser.storage.local.get().then(item => {
chrome.storage.local.get((item) => {
for (const option of options) {
if (defaultOptions[option.id]) {
if (item[option.id] !== undefined) {
document.getElementById(option.id).checked = item[option.id];
} else {
document.getElementById(option.id).checked =
defaultOptions[option.id];
browser.storage.local.set({
chrome.storage.local.set({
[option.id]: defaultOptions[option.id]
});
}
Expand Down Expand Up @@ -515,7 +515,7 @@ const restoreOptions = () => {
};
document.getElementById("openOptions").onclick = e => {
e.preventDefault();
browser.runtime.openOptionsPage();
chrome.runtime.openOptionsPage();
};
document.getElementById("filterInput").onkeyup = () => createList();
});
Expand All @@ -525,7 +525,7 @@ document.addEventListener("DOMContentLoaded", () => {
restoreOptions();
createList();

browser.runtime.onMessage.addListener(message => {
chrome.runtime.onMessage.addListener(message => {
if (message.urlStorage) createList();
});
});

0 comments on commit 9efcc1a

Please sign in to comment.