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

Commit

Permalink
Fix #2184, replace chrome.* APIs with browser.* equivalents
Browse files Browse the repository at this point in the history
  • Loading branch information
ianb committed Mar 10, 2017
1 parent 144678b commit b146357
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 56 deletions.
24 changes: 4 additions & 20 deletions addon/webextension/background/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,18 @@ window.auth = (function () {
let sentryPublicDSN = null;
let abTests = {};

browser.storage.local.get(["registrationInfo", "abTests"], catcher.watchFunction((result) => {
if (browser.runtime.lastError) {
catcher.unhandled(new Error(browser.runtime.lastError.message));
if (! result) {
return;
}
}
catcher.watchPromise(browser.storage.local.get(["registrationInfo", "abTests"]).then((result) => {
if (result.abTests) {
abTests = result.abTests;
}
if (result.registrationInfo) {
registrationInfo = result.registrationInfo;
} else {
registrationInfo = generateRegistrationInfo();
browser.storage.local.set({
console.info("Generating new device authentication ID", registrationInfo);
return browser.storage.local.set({
registrationInfo: registrationInfo
}, () => {
if (browser.runtime.lastError) {
catcher.unhandled(new Error(browser.runtime.lastError.message));
} else {
console.info("Device authentication saved");
}
});
console.info("Generating new device authentication ID", registrationInfo);
}
}));

Expand Down Expand Up @@ -116,11 +104,7 @@ window.auth = (function () {
}
if (responseJson.abTests) {
abTests = responseJson.abTests;
browser.storage.local.set({abTests}, () => {
if (browser.runtime.lastError) {
catcher.unhandled(new Error(browser.runtime.lastError.message));
}
});
catcher.watchPromise(browser.storage.local.set({abTests}));
}
}

Expand Down
7 changes: 2 additions & 5 deletions addon/webextension/background/deviceInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ window.deviceInfo = (function () {
let manifest = browser.runtime.getManifest();

let platformInfo = {};
browser.runtime.getPlatformInfo(function(info) {
if (browser.runtime.lastError) {
catcher.unhandled(new Error(browser.runtime.lastError.message), {context: "getPlatformInfo"});
}
catcher.watchPromise(browser.runtime.getPlatformInfo().then((info) => {
platformInfo = info;
});
}));

return function deviceInfo() {
let match = navigator.userAgent.match(/Chrom(?:e|ium)\/([0-9\.]+)/);
Expand Down
1 change: 1 addition & 0 deletions addon/webextension/background/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ window.main = (function () {
title: "Create Page Shot",
contexts: ["page"]
}, () => {
// Note: unlike most browser.* functions this one does not return a promise
if (browser.runtime.lastError) {
catcher.unhandled(new Error(browser.runtime.lastError.message));
}
Expand Down
58 changes: 27 additions & 31 deletions addon/webextension/background/takeshot.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals communication, shot, main, browser, auth, catcher, analytics, browser */
/* globals communication, shot, main, auth, catcher, analytics, browser */

window.takeshot = (function () {
let exports = {};
Expand Down Expand Up @@ -64,36 +64,32 @@ window.takeshot = (function () {
};
pos.width = pos.right - pos.left;
pos.height = pos.bottom - pos.top;
return new Promise((resolve, reject) => {
return browser.tabs.captureVisibleTab(
null,
{format: "png"},
function (dataUrl) {
if (browser.runtime.lastError) {
catcher.unhandled(new Error(browser.runtime.lastError.message));
}
let image = new Image();
image.src = dataUrl;
image.onload = catcher.watchFunction(() => {
let xScale = image.width / scroll.innerWidth;
let yScale = image.height / scroll.innerHeight;
let canvas = document.createElement("canvas");
canvas.height = pos.height * yScale;
canvas.width = pos.width * xScale;
let context = canvas.getContext("2d");
context.drawImage(
image,
pos.left * xScale, pos.top * yScale,
pos.width * xScale, pos.height * yScale,
0, 0,
pos.width * xScale, pos.height * yScale
);
let result = canvas.toDataURL();
resolve(result);
});
}
);
});
return catcher.watchPromise(browser.tabs.captureVisibleTab(
null,
{format: "png"}
).then((dataUrl) => {
let image = new Image();
image.src = dataUrl;
return new Promise((resolve, reject) => {
image.onload = catcher.watchFunction(() => {
let xScale = image.width / scroll.innerWidth;
let yScale = image.height / scroll.innerHeight;
let canvas = document.createElement("canvas");
canvas.height = pos.height * yScale;
canvas.width = pos.width * xScale;
let context = canvas.getContext("2d");
context.drawImage(
image,
pos.left * xScale, pos.top * yScale,
pos.width * xScale, pos.height * yScale,
0, 0,
pos.width * xScale, pos.height * yScale
);
let result = canvas.toDataURL();
resolve(result);
});
});
}));
}

function uploadShot(shot) {
Expand Down

0 comments on commit b146357

Please sign in to comment.