Skip to content

Commit 1a04afd

Browse files
committed
DO_NOT_MERGE: replace chrome api by browser api
`chrome` api seems not to be correctly handled by Safari but `browser` one should I made the replacement on all `inPageMenu` related content scripts (so not the entire code) This fixes some behaviours (localization) but not all (messaging). So I'm not sure that this is the correct thing to do
1 parent 2d62946 commit 1a04afd

File tree

5 files changed

+42
-46
lines changed

5 files changed

+42
-46
lines changed

src/browser/browserApi.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class BrowserApi {
1414
static async getTabFromCurrentWindowId(): Promise<any> {
1515
return await BrowserApi.tabsQueryFirst({
1616
active: true,
17-
windowId: chrome.windows.WINDOW_ID_CURRENT,
17+
windowId: browser.windows.WINDOW_ID_CURRENT,
1818
});
1919
}
2020

@@ -37,7 +37,7 @@ export class BrowserApi {
3737

3838
static async tabsQuery(options: any): Promise<any[]> {
3939
return new Promise(resolve => {
40-
chrome.tabs.query(options, (tabs: any[]) => {
40+
browser.tabs.query(options).then((tabs: any[]) => {
4141
resolve(tabs);
4242
});
4343
});
@@ -70,8 +70,8 @@ export class BrowserApi {
7070
}
7171

7272
return new Promise<void>(resolve => {
73-
chrome.tabs.sendMessage(tab.id, obj, options, () => {
74-
if (chrome.runtime.lastError) {
73+
browser.tabs.sendMessage(tab.id, obj, options).then(() => {
74+
if (browser.runtime.lastError) {
7575
// Some error happened
7676
}
7777
resolve();
@@ -80,23 +80,23 @@ export class BrowserApi {
8080
}
8181

8282
static getBackgroundPage(): any {
83-
return chrome.extension.getBackgroundPage();
83+
return browser.extension.getBackgroundPage();
8484
}
8585

8686
static getApplicationVersion(): string {
87-
return chrome.runtime.getManifest().version;
87+
return browser.runtime.getManifest().version;
8888
}
8989

9090
static async isPopupOpen(): Promise<boolean> {
91-
return Promise.resolve(chrome.extension.getViews({ type: 'popup' }).length > 0);
91+
return Promise.resolve(browser.extension.getViews({ type: 'popup' }).length > 0);
9292
}
9393

9494
static createNewTab(url: string, extensionPage: boolean = false, active: boolean = true) {
95-
chrome.tabs.create({ url: url, active: active });
95+
browser.tabs.create({ url: url, active: active });
9696
}
9797

9898
static messageListener(name: string, callback: (message: any, sender: any, response: any) => void) {
99-
chrome.runtime.onMessage.addListener((msg: any, sender: any, response: any) => {
99+
browser.runtime.onMessage.addListener((msg: any, sender: any, response: any) => {
100100
callback(msg, sender, response);
101101
});
102102
}
@@ -146,19 +146,19 @@ export class BrowserApi {
146146
}
147147

148148
static getUILanguage(win: Window) {
149-
return chrome.i18n.getUILanguage();
149+
return browser.i18n.getUILanguage();
150150
}
151151

152152
static reloadExtension(win: Window) {
153153
if (win != null) {
154154
return win.location.reload(true);
155155
} else {
156-
return chrome.runtime.reload();
156+
return browser.runtime.reload();
157157
}
158158
}
159159

160160
static reloadOpenWindows() {
161-
const views = chrome.extension.getViews() as Window[];
161+
const views = browser.extension.getViews() as Window[];
162162
views.filter(w => w.location.href != null).forEach(w => {
163163
w.location.reload();
164164
});

src/browser/safariApp.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,13 @@ export class SafariApp {
55
if (!BrowserApi.isSafariApi) {
66
return Promise.resolve(null);
77
}
8-
return new Promise(resolve => {
9-
const now = new Date();
10-
const messageId = now.getTime().toString() + '_' + Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
11-
(browser as any).runtime.sendNativeMessage('io.cozy.pass.desktop', {
12-
id: messageId,
13-
command: command,
14-
data: data,
15-
responseData: null,
16-
}, (response: any) => {
17-
resolve(response);
18-
});
8+
const now = new Date();
9+
const messageId = now.getTime().toString() + '_' + Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
10+
return browser.runtime.sendNativeMessage('io.cozy.pass.desktop', {
11+
id: messageId,
12+
command: command,
13+
data: data,
14+
responseData: null,
1915
});
2016
}
2117
}

src/content/menuCtrler.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ function moveSelection(n) {
431431
}
432432
state.selectedCipher = newCipherNode
433433

434-
chrome.runtime.sendMessage({
434+
browser.runtime.sendMessage({
435435
command : 'bgAnswerMenuRequest',
436436
subcommand : 'menuMoveSelection',
437437
targetCipher : newCipherNode.data.id,
@@ -457,7 +457,7 @@ function getPossibleTypesForField(fieldEl) {
457457
/* --------------------------------------------------------------------- */
458458
// Submit the currently selected cypher for autofill
459459
function submit() {
460-
chrome.runtime.sendMessage({
460+
browser.runtime.sendMessage({
461461
command : 'bgAnswerMenuRequest',
462462
subcommand : 'fillFormWithCipher',
463463
sender : 'menuCtrler',
@@ -470,7 +470,7 @@ menuCtrler.submit = submit
470470
/* --------------------------------------------------------------------- */
471471
// autofill the focused field with the detail of the currently selected cypher
472472
function submitDetail() {
473-
chrome.runtime.sendMessage({
473+
browser.runtime.sendMessage({
474474
command : 'bgAnswerMenuRequest',
475475
subcommand : 'askMenuTofillFieldWithData',
476476
sender : 'menuCtrler',
@@ -576,13 +576,13 @@ function _setIframeURLforMenuType(menuType, isPinLocked, isLocked) {
576576
const hash = '#' + encodeURIComponent(JSON.stringify(state.iFrameHash));
577577
const rand = '?' + Math.floor((Math.random()*1000000)+1)
578578
if (menuType === 'autofillMenu') {
579-
menuEl.src = chrome.runtime.getURL('inPageMenu/menu.html' + rand) + hash
579+
menuEl.src = browser.runtime.getURL('inPageMenu/menu.html' + rand) + hash
580580
} else if (menuType === 'loginMenu') {
581581
let searchParams = ''
582582
if (isPinLocked) searchParams = 'isPinLocked=true'
583583
if (isLocked) searchParams += 'isLocked=true'
584584
if (searchParams) searchParams = '?' + searchParams
585-
menuEl.src = chrome.runtime.getURL('inPageMenu/loginMenu.html' + searchParams + rand) + hash
585+
menuEl.src = browser.runtime.getURL('inPageMenu/loginMenu.html' + searchParams + rand) + hash
586586
}
587587
}
588588

src/inPageMenu/loginMenu.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var panel ,
3636
document.addEventListener('DOMContentLoaded', () => {
3737

3838
// 0- ask rememberedCozyUrl
39-
chrome.runtime.sendMessage({
39+
browser.runtime.sendMessage({
4040
command : 'bgAnswerMenuRequest',
4141
subcommand: 'getRememberedCozyUrl',
4242
sender : 'loginMenu.js',
@@ -84,7 +84,7 @@ document.addEventListener('DOMContentLoaded', () => {
8484
}, false);
8585
} else {
8686
// retrieve i18n values and set elements textcontent
87-
const i18nGetMessage = chrome.i18n.getMessage
87+
const i18nGetMessage = browser.i18n.getMessage
8888
urlLabelTxt = i18nGetMessage('cozyUrl' )
8989
twoFaLabelTxt = i18nGetMessage('verificationCode' )
9090
visiPwdBtn.title = i18nGetMessage('toggleVisibility' )
@@ -115,17 +115,17 @@ document.addEventListener('DOMContentLoaded', () => {
115115
});
116116

117117
// 8- listen to the commands sent by the addon
118-
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
118+
browser.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
119119
// console.log('loginMenu heared msg', msg);
120120
if (msg.command !== 'menuAnswerRequest') return
121121
switch (msg.subcommand) {
122122
case 'loginNOK':
123123
// console.log("loginNOK heard in loginInPageMenu");
124-
setError(chrome.i18n.getMessage('inPageMenuLoginError'))
124+
setError(browser.i18n.getMessage('inPageMenuLoginError'))
125125
break;
126126
case '2faCheckNOK':
127127
// console.log("2faCheckNOK heard in loginInPageMenu");
128-
setError(chrome.i18n.getMessage('inPageMenuLogin2FACheckError'))
128+
setError(browser.i18n.getMessage('inPageMenuLogin2FACheckError'))
129129
adjustMenuHeight()
130130
break;
131131
case 'setRememberedCozyUrl':
@@ -189,7 +189,7 @@ document.addEventListener('DOMContentLoaded', () => {
189189
function adjustMenuHeight() {
190190
if (lastSentHeight === panel.offsetHeight) return
191191
lastSentHeight = panel.offsetHeight
192-
chrome.runtime.sendMessage({
192+
browser.runtime.sendMessage({
193193
command : 'bgAnswerMenuRequest' ,
194194
subcommand: 'setMenuHeight' ,
195195
height : lastSentHeight ,
@@ -227,7 +227,7 @@ async function submit() {
227227
subcommand = 'unlock'
228228
}
229229

230-
chrome.runtime.sendMessage({
230+
browser.runtime.sendMessage({
231231
command : 'bgAnswerMenuRequest',
232232
subcommand: subcommand ,
233233
sender : 'loginMenu.js' ,
@@ -243,9 +243,9 @@ async function submit() {
243243
]
244244

245245
if (translatableMessages.includes(e.message)) {
246-
setError(chrome.i18n.getMessage(e.message))
246+
setError(browser.i18n.getMessage(e.message))
247247
} else {
248-
setError(chrome.i18n.getMessage('errorOccurred'))
248+
setError(browser.i18n.getMessage('errorOccurred'))
249249
}
250250
}
251251
}
@@ -262,7 +262,7 @@ async function submit2fa() {
262262
return;
263263
}
264264

265-
chrome.runtime.sendMessage({
265+
browser.runtime.sendMessage({
266266
command : 'bgAnswerMenuRequest',
267267
subcommand: '2faCheck' ,
268268
sender : 'loginMenu.js' ,
@@ -391,7 +391,7 @@ function setFocusOnEmptyField(){
391391
/* --------------------------------------------------------------------- */
392392
// Request the menu controler to close the iframe of the menu
393393
function close(force) {
394-
chrome.runtime.sendMessage({
394+
browser.runtime.sendMessage({
395395
command : 'bgAnswerMenuRequest',
396396
force : force,
397397
subcommand: 'closeMenu' ,

src/inPageMenu/menu.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ document.addEventListener('DOMContentLoaded', () => {
7171
// }, false);
7272
} else {
7373
// retrieve i18n values and set elements textcontent
74-
lang = chrome.i18n.getUILanguage();
74+
lang = browser.i18n.getUILanguage();
7575
i18nGetMessage = chrome.i18n.getMessage
7676
titleEl.textContent = i18nGetMessage('inPageMenuSelectAnAccount')
7777
}
7878

7979
// 3- listen to the commands and ciphers sent by the addon
80-
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
80+
browser.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
8181
/*
8282
@override by Cozy : this log is very useful for reverse engineering the code, keep it for tests
8383
console.log('menu.js HEARD : ', {
@@ -134,7 +134,7 @@ document.addEventListener('DOMContentLoaded', () => {
134134
})
135135

136136
// 4- request ciphers to the background scripts
137-
chrome.runtime.sendMessage({
137+
browser.runtime.sendMessage({
138138
command : 'bgAnswerMenuRequest',
139139
subcommand: 'getCiphersForTab' ,
140140
sender : 'menu.js' ,
@@ -143,7 +143,7 @@ document.addEventListener('DOMContentLoaded', () => {
143143
// 5- listen to click on the close menu button
144144
const closeIcon = document.querySelector('.close-icon')
145145
closeIcon.addEventListener('click',()=>{
146-
chrome.runtime.sendMessage({
146+
browser.runtime.sendMessage({
147147
command : 'bgAnswerMenuRequest',
148148
subcommand: 'closeMenu' ,
149149
force : true ,
@@ -174,7 +174,7 @@ document.addEventListener('DOMContentLoaded', () => {
174174
/* --------------------------------------------------------------------- */
175175
// Request the background to autofill the page with a cipher
176176
function requestFormFillingWithCipher(cipherId) {
177-
chrome.runtime.sendMessage({
177+
browser.runtime.sendMessage({
178178
command : 'bgAnswerMenuRequest',
179179
subcommand: 'fillFormWithCipher' ,
180180
cipherId : cipherId ,
@@ -186,7 +186,7 @@ function requestFormFillingWithCipher(cipherId) {
186186
/* --------------------------------------------------------------------- */
187187
// Request the background to autofill the page with a cipher
188188
function requestFieldFillingWithData(dataTxt) {
189-
chrome.runtime.sendMessage({
189+
browser.runtime.sendMessage({
190190
command : 'bgAnswerMenuRequest' ,
191191
subcommand: 'fieldFillingWithData' ,
192192
frameId : hostFrameId ,
@@ -328,7 +328,7 @@ function selectFirstVisibleRow() {
328328
function adjustMenuHeight() {
329329
if (lastSentHeight === panel.offsetHeight) return
330330
lastSentHeight = panel.offsetHeight
331-
chrome.runtime.sendMessage({
331+
browser.runtime.sendMessage({
332332
command : 'bgAnswerMenuRequest' ,
333333
subcommand: 'setMenuHeight' ,
334334
height : lastSentHeight ,

0 commit comments

Comments
 (0)