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

Commit

Permalink
Do the first part of #2184, convert all chrome.* APIs to browser.*, b…
Browse files Browse the repository at this point in the history
…ut don't convert to promises
  • Loading branch information
ianb committed Mar 10, 2017
1 parent 87b9ca2 commit 144678b
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 52 deletions.
23 changes: 10 additions & 13 deletions addon/webextension/background/auth.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals chrome */
/* globals browser */
/* globals main, makeUuid, deviceInfo, analytics, catcher, defaultSentryDsn */

window.auth = (function () {
Expand All @@ -10,9 +10,9 @@ window.auth = (function () {
let sentryPublicDSN = null;
let abTests = {};

chrome.storage.local.get(["registrationInfo", "abTests"], catcher.watchFunction((result) => {
if (chrome.runtime.lastError) {
catcher.unhandled(new Error(chrome.runtime.lastError.message));
browser.storage.local.get(["registrationInfo", "abTests"], catcher.watchFunction((result) => {
if (browser.runtime.lastError) {
catcher.unhandled(new Error(browser.runtime.lastError.message));
if (! result) {
return;
}
Expand All @@ -24,11 +24,11 @@ window.auth = (function () {
registrationInfo = result.registrationInfo;
} else {
registrationInfo = generateRegistrationInfo();
chrome.storage.local.set({
browser.storage.local.set({
registrationInfo: registrationInfo
}, () => {
if (chrome.runtime.lastError) {
catcher.unhandled(new Error(chrome.runtime.lastError.message));
if (browser.runtime.lastError) {
catcher.unhandled(new Error(browser.runtime.lastError.message));
} else {
console.info("Device authentication saved");
}
Expand All @@ -45,9 +45,6 @@ window.auth = (function () {
let info = {
deviceId: "anon" + makeUuid() + "",
secret: makeUuid()+"",
// FIXME-chrome: need to figure out the reason the extension was created
// (i.e., startup or install)
//reason,
deviceInfo: JSON.stringify(deviceInfo())
};
return info;
Expand Down Expand Up @@ -119,9 +116,9 @@ window.auth = (function () {
}
if (responseJson.abTests) {
abTests = responseJson.abTests;
chrome.storage.local.set({abTests}, () => {
if (chrome.runtime.lastError) {
catcher.unhandled(new Error(chrome.runtime.lastError.message));
browser.storage.local.set({abTests}, () => {
if (browser.runtime.lastError) {
catcher.unhandled(new Error(browser.runtime.lastError.message));
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions addon/webextension/background/communication.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* globals chrome, catcher */
/* globals browser, catcher */
window.communication = (function () {
let exports = {};

let registeredFunctions = {};

chrome.runtime.onMessage.addListener(catcher.watchFunction((req, sender, sendResponse) => {
browser.runtime.onMessage.addListener(catcher.watchFunction((req, sender, sendResponse) => {
if (! (req.funcName in registeredFunctions)) {
console.error(`Received unknown internal message type ${req.funcName}`);
sendResponse({type: "error", name: "Unknown message type"});
Expand Down
10 changes: 5 additions & 5 deletions addon/webextension/background/deviceInfo.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* globals chrome, catcher */
/* globals browser, catcher */

window.deviceInfo = (function () {
let manifest = chrome.runtime.getManifest();
let manifest = browser.runtime.getManifest();

let platformInfo = {};
chrome.runtime.getPlatformInfo(function(info) {
if (chrome.runtime.lastError) {
catcher.unhandled(new Error(chrome.runtime.lastError.message), {context: "getPlatformInfo"});
browser.runtime.getPlatformInfo(function(info) {
if (browser.runtime.lastError) {
catcher.unhandled(new Error(browser.runtime.lastError.message), {context: "getPlatformInfo"});
}
platformInfo = info;
});
Expand Down
14 changes: 7 additions & 7 deletions addon/webextension/background/loadSelector.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals chrome, catcher */
/* globals browser, catcher */

window.loadSelector = (function () {
// These modules are loaded in order, and some need to be listed before others
Expand All @@ -25,19 +25,19 @@ window.loadSelector = (function () {
let lastPromise = Promise.resolve(null);
scripts.forEach((script) => {
lastPromise = lastPromise.then(() => {
return chrome.tabs.executeScript({
return browser.tabs.executeScript({
file: script
}, () => {
if (chrome.runtime.lastError
&& chrome.runtime.lastError.message != "Script returned non-structured-clonable data") {
catcher.unhandled(chrome.runtime.lastError, {script: script});
console.error("Error loading script", script, ":", chrome.runtime.lastError);
if (browser.runtime.lastError
&& browser.runtime.lastError.message != "Script returned non-structured-clonable data") {
catcher.unhandled(browser.runtime.lastError, {script: script});
console.error("Error loading script", script, ":", browser.runtime.lastError);
}
});
});
});
return lastPromise.then(() => {
console.log("finished loading scripts:", scripts.join(" "), "->", chrome.runtime.lastError || "no error");
console.log("finished loading scripts:", scripts.join(" "), "->", browser.runtime.lastError || "no error");
});
}

Expand Down
23 changes: 10 additions & 13 deletions addon/webextension/background/main.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* globals chrome, console, XMLHttpRequest, Image, document, setTimeout, navigator */
/* globals browser, console, XMLHttpRequest, Image, document, setTimeout, navigator */
/* globals loadSelector, analytics, communication, catcher, makeUuid */
window.main = (function () {
let exports = {};

const pasteSymbol = (window.navigator.platform.match(/Mac/i)) ? "\u2318" : "Ctrl";
const { sendEvent } = analytics;

let manifest = chrome.runtime.getManifest();
let manifest = browser.runtime.getManifest();
let backend;

exports.setBackend = function (newBackend) {
Expand All @@ -25,30 +25,27 @@ window.main = (function () {
}
}

chrome.runtime.onInstalled.addListener(function () {
});

chrome.browserAction.onClicked.addListener(function(tab) {
browser.browserAction.onClicked.addListener(function(tab) {
if(tab.url.match(/about:(newtab|blank)/i)) {
sendEvent("goto-myshots", "about-newtab");
chrome.tabs.update({url: backend + "/shots"});
browser.tabs.update({url: backend + "/shots"});
} else {
sendEvent("start-shot", "toolbar-pageshot-button");
catcher.watchPromise(loadSelector());
}
});

chrome.contextMenus.create({
browser.contextMenus.create({
id: "create-pageshot",
title: "Create Page Shot",
contexts: ["page"]
}, () => {
if (chrome.runtime.lastError) {
catcher.unhandled(new Error(chrome.runtime.lastError.message));
if (browser.runtime.lastError) {
catcher.unhandled(new Error(browser.runtime.lastError.message));
}
});

chrome.contextMenus.onClicked.addListener(catcher.watchFunction((info, tab) => {
browser.contextMenus.onClicked.addListener(catcher.watchFunction((info, tab) => {
if (! tab) {
// Not in a page/tab context, ignore
return;
Expand All @@ -65,13 +62,13 @@ window.main = (function () {
});

communication.register("openMyShots", () => {
chrome.tabs.create({url: backend + "/shots"});
browser.tabs.create({url: backend + "/shots"});
});

communication.register("openShot", ({url, copied}) => {
if (copied) {
const id = makeUuid();
chrome.notifications.create(id, {
browser.notifications.create(id, {
type: "basic",
iconUrl: "../icons/clipboard-32.png",
title: "Link Copied",
Expand Down
4 changes: 2 additions & 2 deletions addon/webextension/background/senderror.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals chrome, communication, makeUuid, Raven, catcher, auth */
/* globals browser, communication, makeUuid, Raven, catcher, auth */

window.errorpopup = (function () {
let exports = {};
Expand Down Expand Up @@ -57,7 +57,7 @@ window.errorpopup = (function () {
message = error.message;
}
}
chrome.notifications.create(id, {
browser.notifications.create(id, {
type: "basic",
// FIXME: need iconUrl for an image, see #2239
title,
Expand Down
8 changes: 4 additions & 4 deletions addon/webextension/background/takeshot.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals communication, shot, main, chrome, auth, catcher, analytics, browser */
/* globals communication, shot, main, browser, auth, catcher, analytics, browser */

window.takeshot = (function () {
let exports = {};
Expand Down Expand Up @@ -65,12 +65,12 @@ window.takeshot = (function () {
pos.width = pos.right - pos.left;
pos.height = pos.bottom - pos.top;
return new Promise((resolve, reject) => {
return chrome.tabs.captureVisibleTab(
return browser.tabs.captureVisibleTab(
null,
{format: "png"},
function (dataUrl) {
if (chrome.runtime.lastError) {
catcher.unhandled(new Error(chrome.runtime.lastError.message));
if (browser.runtime.lastError) {
catcher.unhandled(new Error(browser.runtime.lastError.message));
}
let image = new Image();
image.src = dataUrl;
Expand Down
4 changes: 2 additions & 2 deletions addon/webextension/selector/ui.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals window, document, console, chrome */
/* globals window, document, console, browser */
/* globals util, catcher, inlineSelectionCss */

window.ui = (function () { // eslint-disable-line no-unused-vars
Expand Down Expand Up @@ -44,7 +44,7 @@ window.ui = (function () { // eslint-disable-line no-unused-vars
}

let substitutedCss = inlineSelectionCss.replace(/MOZ_EXTENSION([^\"]+)/g, (match, filename) => {
return chrome.extension.getURL(filename);
return browser.extension.getURL(filename);
});

function makeEl(tagName, className) {
Expand Down
8 changes: 4 additions & 4 deletions addon/webextension/site-helper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals document, alert, chrome, setInterval, clearTimeout */
/* globals document, alert, browser, setInterval, clearTimeout */

function sendEvent(type, detail) {
let event = document.createEvent("CustomEvent");
Expand All @@ -7,7 +7,7 @@ function sendEvent(type, detail) {
}

document.addEventListener("has-saved-shot", (event) => {
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "has-saved-shot",
id: event.detail
}, (response) => {
Expand All @@ -16,7 +16,7 @@ document.addEventListener("has-saved-shot", (event) => {
}, false);

document.addEventListener("request-saved-shot", (event) => {
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "request-saved-shot",
id: event.detail
}, (response) => {
Expand All @@ -26,7 +26,7 @@ document.addEventListener("request-saved-shot", (event) => {
}, false);

document.addEventListener("remove-saved-shot", (event) => {
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "remove-saved-shot",
id: event.detail
});
Expand Down

0 comments on commit 144678b

Please sign in to comment.