Skip to content

Commit

Permalink
extension: migrate all chrome. calls to browser. in preparation of ty…
Browse files Browse the repository at this point in the history
…pescript migration
  • Loading branch information
karlicoss committed May 20, 2024
1 parent e7adfb1 commit a00c32d
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 38 deletions.
15 changes: 15 additions & 0 deletions extension/__mocks__/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ const mockBrowser = {
bookmarks: {
getTree: jest.fn(),
},
storage: {
sync: {
// meh.
get: (name, res) => {
res({'options': {
host: 'http://badhost:43210', // some random port, to cause it fail
}})
}
},
},
runtime: {
lastError: null,
getManifest : () => { return {version: 'whatever'} },
getPlatformInfo: async () => {},
},
}

export default mockBrowser
2 changes: 2 additions & 0 deletions extension/flow-typed/webextension-polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ declare var browser: {


declare module "webextension-polyfill" {
declare var browserAction: chrome$browserAction;
declare var pageAction : chrome$pageAction;
declare var bookmarks : browser$bookmarks;
declare var history : browser$history;
declare var storage : browser$storage;
Expand Down
4 changes: 3 additions & 1 deletion extension/src/api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* @flow */
import browser from "webextension-polyfill"

/*
* Communication with backend
Expand Down Expand Up @@ -50,7 +51,8 @@ export async function queryBackendCommon<R>(params: any, endp: string): Promise<
}

const endpoint = `${opts.host}/${endp}`
params['client_version'] = chrome.runtime.getManifest().version
// $FlowFixMe
params['client_version'] = browser.runtime.getManifest().version

function with_stack(e: Error): Error {
const stack = []
Expand Down
36 changes: 22 additions & 14 deletions extension/src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ function actions(): Array<Action> {
// but we can use pageAction to show at least some (default) icon in some circumstances

// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Differences_between_desktop_and_Android#User_interface
const res: Array<Action> = [chrome.browserAction]
const res: Array<Action> = [browser.browserAction]

// need to be defensive, it's only for mobile firefox
if (chrome.pageAction) {
res.push(chrome.pageAction)
if (browser.pageAction) {
res.push(browser.pageAction)
} else {
// this is a bit backwards because we need to register callbacks synchronously
// otherwise isn't not working well after background page unloads
Expand Down Expand Up @@ -233,7 +233,7 @@ async function updateState(tab: TabUrl): Promise<void> {
* otherwise, we're relying on 'mobile_sidebar_injector' to open the sidebar
*/
if (await isMobile()) {
const action = chrome.pageAction;
const action = browser.pageAction;
const interesting = [
'images/ic_visited_48.png',
'images/ic_relatives_48.png',
Expand Down Expand Up @@ -295,7 +295,8 @@ async function updateState(tab: TabUrl): Promise<void> {

function sendSidebarMessage(tabId: number, message: any) {
// ugh.. just so I don't shoot myself in the foot again with using runtime.sendMessage...
chrome.tabs.sendMessage(tabId, message)
// $FlowFixMe
browser.tabs.sendMessage(tabId, message)
}


Expand Down Expand Up @@ -601,8 +602,10 @@ async function handleOpenSearch(p: SearchPageParams = {}) {
params.append(k, v)
}
const ps = params.toString()
const search_url = chrome.runtime.getURL('search.html') + (ps.length == 0 ? '' : '?' + ps)
chrome.tabs.create({url: search_url})
// $FlowFixMe
const search_url = browser.runtime.getURL('search.html') + (ps.length == 0 ? '' : '?' + ps)
// $FlowFixMe
browser.tabs.create({url: search_url})
// TODO get current tab url and pass as get parameter?
}

Expand Down Expand Up @@ -967,7 +970,8 @@ function initContextMenus(): void {
*/
browser.contextMenus.removeAll().then(() => {
for (const {id: id, title: title, parentId: parentId, contexts: contexts} of MENUS) {
chrome.contextMenus.create({
// $FlowFixMe
browser.contextMenus.create({
id: id,
parentId: parentId,
title: title,
Expand All @@ -976,7 +980,8 @@ function initContextMenus(): void {
}

for (const {id: id, title: title, parentId: parentId, contexts: contexts} of TOGGLES) {
chrome.contextMenus.create({
// $FlowFixMe
browser.contextMenus.create({
id: id,
parentId: parentId,
title: title,
Expand Down Expand Up @@ -1013,7 +1018,8 @@ function updateContextMenus(opts: Options): void {
return
}
for (const {id: id, checker: checker} of TOGGLES) {
chrome.contextMenus.update(
// $FlowFixMe
browser.contextMenus.update(
id,
{checked: checker(opts)},
)
Expand All @@ -1026,14 +1032,15 @@ function initBackground(): void {
// otherwise doesn't work well with background page suspension

// $FlowFixMe
chrome.runtime.onMessage.addListener(onMessageCallback)
browser.runtime.onMessage.addListener(onMessageCallback)

registerActions()

// need to be defensive since commands API isn't available under mobile browser
if (chrome.commands) {
// $FlowFixMe
if (browser.commands) {
// $FlowFixMe // err, complains at Promise but nevertheless works
chrome.commands.onCommand.addListener(onCommandCallback)
browser.commands.onCommand.addListener(onCommandCallback)
} else {
isMobile().then(mobile => {
if (!mobile) {
Expand All @@ -1046,7 +1053,8 @@ function initBackground(): void {
}


chrome.runtime.onMessage.addListener((info: any, _: chrome$MessageSender) => {
// $FlowFixMe
browser.runtime.onMessage.addListener((info: any, _: chrome$MessageSender) => {
// see selenium_bridge.js
if (info === 'selenium-bridge-_execute_browser_action') {
handleToggleSidebar()
Expand Down
5 changes: 4 additions & 1 deletion extension/src/display.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* @flow */
import browser from "webextension-polyfill"

import type {Url, Src, Locator} from './common';
import {Methods, unwrap, safeSetInnerHTML} from './common';
import type {Options} from './options'
Expand Down Expand Up @@ -140,7 +142,8 @@ export class Binder {
dt_c.onclick = () => {
// TODO not sure about floor...
const utc_timestamp_s = Math.floor(timestamp.getTime() / 1000);
chrome.runtime.sendMessage({
// $FlowFixMe
browser.runtime.sendMessage({
method : Methods.SEARCH_VISITS_AROUND,
utc_timestamp_s: utc_timestamp_s,
});
Expand Down
3 changes: 2 additions & 1 deletion extension/src/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {getOptions} from './options';
// last resort.. because these could be annoying (also might not make sense to display globally)
// only using it when there is no other context (e.g. current tab) to show a notification
export function desktopNotify(message: string, priority: number=0) {
chrome.notifications.create({
// $FlowFixMe
browser.notifications.create({
'type' : "basic",
'title' : "promnesia",
'message' : message,
Expand Down
6 changes: 4 additions & 2 deletions extension/src/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ class Sidebar {
const cdoc = frame.contentDocument;
const head = unwrap(cdoc.head);

const sidebar_css = chrome.runtime.getURL("sidebar.css")
// $FlowFixMe
const sidebar_css = browser.runtime.getURL("sidebar.css")
const link = cdoc.createElement("link");
link.href = sidebar_css;
link.type = "text/css";
Expand Down Expand Up @@ -651,7 +652,8 @@ const onMessageListener = (msg: any, _: chrome$MessageSender) => {
if (window.promnesia_haslistener == null) {
window.promnesia_haslistener = true
console.debug(`[promnesia] [sidebar-${UUID}] registering callbacks`)
chrome.runtime.onMessage.addListener(onMessageListener)
// $FlowFixMe
browser.runtime.onMessage.addListener(onMessageListener)
} else {
console.debug(`[promnesia] [sidebar-${UUID}] skipping callback registration, they are already present`)
}
Expand Down
21 changes: 2 additions & 19 deletions extension/tests/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,8 @@

import {setOptions, getOptions} from '../src/options'

global.chrome = {
storage: {
sync: {
// meh.
get: (name, res) => {
res({'options': {
host: 'http://badhost:43210', // some random port, to cause it fail
}})
}
},
},
runtime: {
lastError: null,
getManifest : () => { return {version: 'whatever'} },
},
}

import mockBrowser from "../__mocks__/browser"
global.chrome = mockBrowser

test('options', async () => {
// shouldn't crash at least..
Expand All @@ -50,8 +35,6 @@ test('visits', async() => {

import {allsources} from '../src/sources'

import mockBrowser from "../__mocks__/browser"

// meh.
mockBrowser.history.getVisits.mockImplementation(async (obj) => [])
mockBrowser.history.search .mockImplementation(async (obj) => [])
Expand Down

0 comments on commit a00c32d

Please sign in to comment.