Skip to content
Open
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
56 changes: 29 additions & 27 deletions webextension/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function logError(error) {
console.error(error.message);
}

// browser.action.onClicked.addListener(onContextItem);

/**
* Callback for activation of the extension toolbar icon
Expand All @@ -59,11 +60,12 @@ function toggleAction(tab = null) {
try {
// Disable on "about:" pages
if (_ABOUT.test(tab.url))
browser.browserAction.disable(tab.id);
else
browser.browserAction.enable(tab.id);
browser.action.disable(tab.id);
else {
browser.action.enable(tab.id);
}
} catch {
browser.browserAction.disable();
browser.action.disable();
}
}

Expand All @@ -90,7 +92,7 @@ async function postMessage(message) {


/**
* Forward a message from the browserAction popup to the NMH
* Forward a message from the action popup to the NMH
*
* @param {object} message - A message from the NMH to forward
* @param {*} sender - A message from the NMH to forward
Expand All @@ -106,7 +108,7 @@ async function onPopupMessage(message, sender) {


/**
* Forward a message from the NMH to the browserAction popup
* Forward a message from the NMH to the action popup
*
* @param {object} message - A message from the NMH to forward
*/
Expand Down Expand Up @@ -177,15 +179,15 @@ async function createContextMenu(tab) {
title: browser.i18n.getMessage('shareMessage'),
parentId: device.id,
contexts: _CONTEXTS,
onclick: onContextItem,
// onclick: onContextItem,
});

await browser.contextMenus.create({
id: `${device.id}:telephony`,
title: browser.i18n.getMessage('smsMessage'),
parentId: device.id,
contexts: _CONTEXTS,
onclick: onContextItem,
// onclick: onContextItem,
});
} else {
let pluginAction, pluginName;
Expand All @@ -206,7 +208,7 @@ async function createContextMenu(tab) {
),
parentId: 'contextMenuMultipleDevices',
contexts: _CONTEXTS,
onclick: onContextItem,
// onclick: onContextItem,
});
}
}
Expand All @@ -227,15 +229,15 @@ async function createContextMenu(tab) {
title: browser.i18n.getMessage('shareMessage'),
parentId: device.id,
contexts: _CONTEXTS,
onclick: onContextItem,
// onclick: onContextItem,
});

await browser.contextMenus.create({
id: `${device.id}:telephony`,
title: browser.i18n.getMessage('smsMessage'),
parentId: device.id,
contexts: _CONTEXTS,
onclick: onContextItem,
// onclick: onContextItem,
});
} else {
let pluginAction, pluginName;
Expand All @@ -255,7 +257,7 @@ async function createContextMenu(tab) {
[device.name, pluginName]
),
contexts: _CONTEXTS,
onclick: onContextItem,
// onclick: onContextItem,
});
}
}
Expand Down Expand Up @@ -293,10 +295,10 @@ async function onPortMessage(message) {
forwardPortMessage(message);

//
const tabs = await browser.tabs.query({
active: true,
const tabs = (await chrome.tabs.query({
// active: true,
currentWindow: true,
});
})).filter(tab => tab.active);

createContextMenu(tabs[0]);
} catch (e) {
Expand All @@ -312,22 +314,22 @@ async function onDisconnect() {
try {
State.connected = false;
State.port = null;
browser.browserAction.setBadgeText({text: '\u26D4'});
browser.browserAction.setBadgeBackgroundColor({color: [198, 40, 40, 255]});
browser.action.setBadgeText({text: '\u26D4'});
browser.action.setBadgeBackgroundColor({color: [198, 40, 40, 255]});
forwardPortMessage({type: 'connected', data: false});

// Clear context menu
await browser.contextMenus.removeAll();

// Disconnected, cancel back-off reset
if (typeof reconnectResetTimer === 'number') {
window.clearTimeout(reconnectResetTimer);
clearTimeout(reconnectResetTimer);
reconnectResetTimer = null;
}

// Don't queue more than one reconnect
// // Don't queue more than one reconnect
if (typeof reconnectTimer === 'number') {
window.clearTimeout(reconnectTimer);
clearTimeout(reconnectTimer);
reconnectTimer = null;
}

Expand All @@ -338,7 +340,7 @@ async function onDisconnect() {
}

// Exponential back-off on reconnect
reconnectTimer = window.setTimeout(connect, reconnectDelay);
reconnectTimer = setTimeout(connect, reconnectDelay);
reconnectDelay *= 2;
} catch (e) {
logError(e);
Expand All @@ -354,11 +356,11 @@ async function connect() {
State.port = browser.runtime.connectNative('org.gnome.shell.extensions.gsconnect');

// Clear the badge and tell the popup we're disconnected
browser.browserAction.setBadgeText({text: ''});
browser.browserAction.setBadgeBackgroundColor({color: [0, 0, 0, 0]});
browser.action.setBadgeText({text: ''});
browser.action.setBadgeBackgroundColor({color: [0, 0, 0, 0]});

// Reset the back-off delay if we stay connected
reconnectResetTimer = window.setTimeout(() => {
reconnectResetTimer = setTimeout(() => {
reconnectDelay = 100;
}, reconnectDelay * 0.9);

Expand All @@ -372,10 +374,10 @@ async function connect() {
}


// Forward messages from the browserAction popup
// Forward messages from the action popup
browser.runtime.onMessage.addListener(onPopupMessage);

// Keep browserAction up to date
// Keep action up to date
browser.tabs.onActivated.addListener((info) => {
browser.tabs.get(info.tabId).then(toggleAction);
});
Expand All @@ -397,7 +399,7 @@ browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {


/**
* Startup: set initial state of the browserAction and try to connect
* Startup: set initial state of the action and try to connect
*/
toggleAction();
connect();
14 changes: 7 additions & 7 deletions webextension/js/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function getDeviceElement(device) {
shareButton.title = browser.i18n.getMessage('shareMessage');
shareButton.addEventListener(
'click',
() => sendUrl(device.id, 'share', URL)
() => sendUrl(device.id, 'share', TARGET_URL)
);
deviceElement.appendChild(shareButton);
}
Expand All @@ -94,7 +94,7 @@ function getDeviceElement(device) {
telephonyButton.title = browser.i18n.getMessage('smsMessage');
telephonyButton.addEventListener(
'click',
() => sendUrl(device.id, 'telephony', URL)
() => sendUrl(device.id, 'telephony', TARGET_URL)
);
deviceElement.appendChild(telephonyButton);
}
Expand Down Expand Up @@ -146,7 +146,7 @@ function onPortMessage(message, sender) {
try {
// console.log(`WebExtension-popup RECV: ${JSON.stringify(message)}`);

if (sender.url.includes('/background.html')) {
// if (sender.url.includes('/background.html')) {
if (message.type === 'connected') {
CONNECTED = message.data;
} else if (message.type === 'devices') {
Expand All @@ -155,7 +155,7 @@ function onPortMessage(message, sender) {
}

setPopup();
}
// }
} catch (e) {
logError(e);
}
Expand All @@ -167,10 +167,10 @@ function onPortMessage(message, sender) {
*/
async function onPopup() {
try {
const tabs = await browser.tabs.query({
active: true,
const tabs = (await browser.tabs.query({
// active: true,
currentWindow: true,
});
})).filter(tab => tab.active);

if (tabs.length)
TARGET_URL = tabs[0].url;
Expand Down
4 changes: 4 additions & 0 deletions webextension/js/webworker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
self.importScripts(
"browser-polyfill.min.js",
"background.js"
)
9 changes: 6 additions & 3 deletions webextension/manifest.chrome.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"manifest_version": 2,
"manifest_version": 3,
"name": "__MSG_extensionName__",
"description": "__MSG_extensionDescription__",
"version": "8",
"homepage_url": "https://github.com/GSConnect/gnome-shell-extension-gsconnect/wiki",
"default_locale": "en",
"browser_action": {
"action": {
"default_title": "__MSG_extensionName__",
"default_popup": "popup.html",
"default_icon": {
Expand All @@ -20,13 +20,16 @@
}
},
"background": {
"page": "background.html"
"service_worker": "js/webworker.js"
},
"permissions": [
"nativeMessaging",
"tabs",
"contextMenus"
],
"host_permissions": [
"*://*/*"
],
"icons": {
"256": "images/gsconnect-256.png",
"128": "images/gsconnect-128.png",
Expand Down
Loading