Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 20 additions & 44 deletions cookie-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,60 +85,36 @@ import { PermissionHandler } from './interface/lib/permissionHandler.js';
const getAllCookiesParams = {
url: request.params.url,
};
if (browserDetector.supportsPromises()) {
browserDetector
.getApi()
.cookies.getAll(getAllCookiesParams)
.then(sendResponse);
} else {
browserDetector
.getApi()
.cookies.getAll(getAllCookiesParams, sendResponse);
}
browserDetector
.getApi()
.cookies.getAll(getAllCookiesParams)
.then(sendResponse);
return true;
}
case 'saveCookie': {
if (browserDetector.supportsPromises()) {
browserDetector
.getApi()
.cookies.set(request.params.cookie)
.then(
cookie => {
sendResponse(null, cookie);
},
error => {
console.error('Failed to create cookie', error);
sendResponse(error.message, null);
}
);
} else {
browserDetector
.getApi()
.cookies.set(request.params.cookie, cookie => {
if (cookie) {
sendResponse(null, cookie);
} else {
const error = browserDetector.getApi().runtime.lastError;
console.error('Failed to create cookie', error);
sendResponse(error.message, cookie);
}
});
}
browserDetector
.getApi()
.cookies.set(request.params.cookie)
.then(
cookie => {
sendResponse(null, cookie);
},
error => {
console.error('Failed to create cookie', error);
sendResponse(error.message, null);
}
);
return true;
}
case 'removeCookie': {
const removeParams = {
name: request.params.name,
url: request.params.url,
};
if (browserDetector.supportsPromises()) {
browserDetector
.getApi()
.cookies.remove(removeParams)
.then(sendResponse);
} else {
browserDetector.getApi().cookies.remove(removeParams, sendResponse);
}
browserDetector
.getApi()
.cookies.remove(removeParams)
.then(sendResponse);
return true;
}
case 'permissionsContains': {
Expand Down
68 changes: 24 additions & 44 deletions interface/devtools/cookieHandlerDevtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,50 +39,40 @@ export class CookieHandlerDevtools extends GenericCookieHandler {

/**
* Gets all the cookies for the current tab.
* @param {function} callback
* @return {Promise}
*/
getAllCookies(callback) {
this.sendMessage(
'getAllCookies',
{
url: this.currentTab.url,
storeId: this.currentTab.cookieStoreId,
},
callback
);
async getAllCookies() {
return this.sendMessage('getAllCookies', {
url: this.currentTab.url,
storeId: this.currentTab.cookieStoreId,
});
}

/**
* Saves a cookie. This can either create a new cookie or modify an existing
* one.
* @param {Cookie} cookie Cookie's data.
* @param {string} url The url to attach the cookie to.
* @param {function} callback
* @return {Promise}
*/
saveCookie(cookie, url, callback) {
this.sendMessage(
'saveCookie',
{ cookie: this.prepareCookie(cookie, url) },
callback
);
async saveCookie(cookie, url) {
return this.sendMessage('saveCookie', {
cookie: this.prepareCookie(cookie, url),
});
}

/**
* Removes a cookie from the browser.
* @param {string} name The name of the cookie to remove.
* @param {string} url The url that the cookie is attached to.
* @param {function} callback
* @return {Promise}
*/
removeCookie(name, url, callback) {
this.sendMessage(
'removeCookie',
{
name: name,
url: url,
storeId: this.currentTab.cookieStoreId,
},
callback
);
async removeCookie(name, url) {
return this.sendMessage('removeCookie', {
name: name,
url: url,
storeId: this.currentTab.cookieStoreId,
});
}

/**
Expand Down Expand Up @@ -135,9 +125,7 @@ export class CookieHandlerDevtools extends GenericCookieHandler {
*/
updateCurrentTab = callback => {
const self = this;
this.sendMessage(
'getCurrentTab',
null,
this.sendMessage('getCurrentTab', null).then(
function (tabInfo) {
const newTab =
tabInfo[0].id !== self.currentTabId ||
Expand All @@ -161,19 +149,11 @@ export class CookieHandlerDevtools extends GenericCookieHandler {
* Sends a message to the background script.
* @param {string} type The type of the message.
* @param {object} params The payload of the message
* @param {function} callback
* @param {function} errorCallback
* @return {Promise}
*/
sendMessage(type, params, callback, errorCallback) {
if (this.browserDetector.supportsPromises()) {
this.browserDetector
.getApi()
.runtime.sendMessage({ type: type, params: params })
.then(callback, errorCallback);
} else {
this.browserDetector
.getApi()
.runtime.sendMessage({ type: type, params: params }, callback);
}
sendMessage(type, params) {
return this.browserDetector
.getApi()
.runtime.sendMessage({ type: type, params: params });
}
}
15 changes: 3 additions & 12 deletions interface/devtools/permissionHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,8 @@ export class PermissionHandler {
* @return {Promise}
*/
sendMessage(type, params) {
const self = this;
if (this.browserDetector.supportsPromises()) {
return this.browserDetector
.getApi()
.runtime.sendMessage({ type: type, params: params });
} else {
return new Promise(function (resolve) {
self.browserDetector
.getApi()
.runtime.sendMessage({ type: type, params: params }, resolve);
});
}
return this.browserDetector
.getApi()
.runtime.sendMessage({ type: type, params: params });
}
}
20 changes: 1 addition & 19 deletions interface/lib/browserDetector.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,9 @@ export class BrowserDetector {
*/
constructor() {
console.log('constructing a browserDetector');
this.namespace = chrome || window.browser || window.chrome;
this.supportPromises = false;
this.namespace = window.browser || chrome || window.chrome;
this.supportSidePanel = false;

try {
this.supportPromises =
this.namespace.runtime.getPlatformInfo() instanceof Promise;
console.info('Promises support: ', this.supportPromises);
} catch (e) {
/* empty */
}

try {
this.supportSidePanel = typeof this.getApi().sidePanel !== 'undefined';
console.info('SidePanel support: ', this.supportSidePanel);
Expand Down Expand Up @@ -79,15 +70,6 @@ export class BrowserDetector {
return Env.browserName === Browsers.Safari;
}

/**
* Checks if the current browser's API supports promises.
* @return {boolean} true if the current browser's API supports promises,
* otherwise false.
*/
supportsPromises() {
return this.supportPromises;
}

/**
* Checks if the current browser supports the Sidepanel API.
* @return {boolean} true if the current browser supports the Sidepanel API,
Expand Down
Loading
Loading