Skip to content

Commit

Permalink
Notify changes with a desltop notification instead of a startup tab
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Aug 5, 2020
1 parent 6240a05 commit 54baf3c
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 35 deletions.
8 changes: 8 additions & 0 deletions webextensions/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
"notPermitted_clipboardWrite_message": { "message": "Failed to get permissions to write data to the clipboard. Please grant permissions via the configurations of Multiple Tab Handler." },


"startup_notification_title_legacy": { "message": "About revived Multiple Tab Handler" },
"startup_notification_message_legacy": { "message": "The operation to select tabs is totally changed. Please click here to see explanations." },
"startup_notification_title_installed": { "message": "For newcomer of Multiple Tab Handler" },
"startup_notification_message_installed": { "message": "Some extra permissions are required for more natural user experiences. Please click here to grant them." },
"startup_notification_title_updated": { "message": "Noticeable Changes on Multiple Tab Handler" },
"startup_notification_message_updated": { "message": "Some changes may affect to your use case. Please click here to see explanations." },


"panelTitle": { "message": "Select Tabs by Dragging" },
"panelCaptionTop": { "message": "Select Tabs" },
"panelCaptionBottom": { "message": "And Right-Click to Choose Action" },
Expand Down
8 changes: 8 additions & 0 deletions webextensions/_locales/ja/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
"notPermitted_clipboardWrite_message": { "message": "クリップボードへのデータの書き込みに必要な権限を取得できませんでした。Multiple Tab Handlerの設定画面で権限を設定して下さい。" },


"startup_notification_title_legacy": { "message": "生まれ変わったMultiple Tab Handlerについて" },
"startup_notification_message_legacy": { "message": "タブの選択法法が大幅に変わりました。ここをクリックして補足説明を確認してください。" },
"startup_notification_title_installed": { "message": "Multiple Tab Handlerを使い始める方へ" },
"startup_notification_message_installed": { "message": "より自然な操作感を得るためには、追加の権限を許可する必要があります。ここをクリックして権限を設定してください。" },
"startup_notification_title_updated": { "message": "Multiple Tab Handlerの動作が変わりました" },
"startup_notification_message_updated": { "message": "操作感に影響する大きな変更がありました。ここをクリックして補足説明を確認してください。" },


"panelTitle": { "message": "タブをドラッグして選択" },
"panelCaptionTop": { "message": "タブを選択してください" },
"panelCaptionBottom": { "message": "その後、右クリックして操作を選択" },
Expand Down
48 changes: 14 additions & 34 deletions webextensions/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import {
log,
configs,
notify,
handleMissingReceiverError
} from '/common/common.js';
import * as Constants from '/common/constants.js';
Expand Down Expand Up @@ -435,24 +436,20 @@ async function notifyNewFeatures() {
*/

const featuresVersion = kFEATURES_VERSION /*+ featuresVersionOffset*/;
const isInitialInstall = configs.notifiedFeaturesVersion == 0;

if (configs.notifiedFeaturesVersion >= featuresVersion)
return false;
configs.notifiedFeaturesVersion = featuresVersion;

const tab = await browser.tabs.create({
url: browser.extension.getURL('resources/notify-features.html'),
active: true
});
const title = `${browser.i18n.getMessage('extensionName')} ${browser.runtime.getManifest().version}`
const description = browser.i18n.getMessage('message_newFeatures_description');
browser.tabs.executeScript(tab.id, {
code: `
document.querySelector('#title').textContent = document.title = ${JSON.stringify(title)};
document.querySelector('#description').innerHTML = ${JSON.stringify(description)};
location.replace('data:text/html,' + encodeURIComponent(document.documentElement.innerHTML));
`
const suffix = isInitialInstall ? 'installed' : 'updated';
notify({
url: browser.extension.getURL(`resources/notify-features.html?${suffix}`),
title: browser.i18n.getMessage(`startup_notification_title_${suffix}`),
message: browser.i18n.getMessage(`startup_notification_message_${suffix}`),
timeout: 90 * 1000
});

return true;
}

Expand All @@ -461,28 +458,11 @@ async function notifyUpdatedFromLegacy() {
return false;
configs.shouldNotifyUpdatedFromLegacyVersion = false;

const tab = await browser.tabs.create({
url: browser.extension.getURL('resources/notify-features.html'),
active: true
});
const title = `${browser.i18n.getMessage('extensionName')} ${browser.runtime.getManifest().version}`
const description = browser.i18n.getMessage('message_updatedFromLegacy_description');
browser.tabs.executeScript(tab.id, {
code: `
document.querySelector('#title').textContent = document.title = ${JSON.stringify(title)};
document.querySelector('#description').innerHTML = ${JSON.stringify(description)};
location.replace('data:text/html,' + encodeURIComponent(document.documentElement.innerHTML));
`
});

browser.runtime.onMessage.addListener(function onMessage(message, _sender) {
if (message &&
typeof message.type == 'string' &&
message.type == Constants.kCOMMAND_NOTIFY_PANEL_SHOWN) {
browser.runtime.onMessage.removeListener(onMessage);
browser.tabs.remove(tab.id)
.catch(_e => {}); // ignore already closed tab
}
notify({
url: browser.extension.getURL(`resources/notify-features.html?legacy`),
title: browser.i18n.getMessage(`startup_notification_title_legacy`),
message: browser.i18n.getMessage(`startup_notification_message_legacy`),
timeout: 90 * 1000
});
}

Expand Down
2 changes: 1 addition & 1 deletion webextensions/common/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export async function wait(task = 0, timeout = 0) {
export async function notify({ icon, title, message, timeout, url } = {}) {
const id = await browser.notifications.create({
type: 'basic',
iconUrl: icon,
iconUrl: icon || browser.extension.getURL(`resources/64x64.svg`),
title,
message
});
Expand Down
41 changes: 41 additions & 0 deletions webextensions/resources/64x64-dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions webextensions/resources/64x64-light.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions webextensions/resources/64x64.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions webextensions/resources/module/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*eslint-env commonjs*/
/*eslint quote-props: ['error', "always"] */

'use strict';

module.exports = {
'extends': [
'../../tools/eslint/for-module.js',
],
};
20 changes: 20 additions & 0 deletions webextensions/resources/module/notify-features.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
'use strict';

window.addEventListener('DOMContentLoaded', () => {
const type = /(legacy|installed|updated)/i.test(location.search) && String(RegExp.$1).toLowerCase();

const title = `${browser.i18n.getMessage('extensionName')} ${browser.runtime.getManifest().version}`;
const description = browser.i18n.getMessage(
type == 'legacy' ?
'message_updatedFromLegacy_description' :
'message_newFeatures_description'
);

document.querySelector('#title').textContent = document.title = title;
document.querySelector('#description').innerHTML = description;
}, { once: true });
1 change: 1 addition & 0 deletions webextensions/resources/notify-features.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<meta charset="UTF-8">
<link rel="icon" href="./16x16-light.svg">
<title></title>
<script type="module" src="./module/notify-features.js"></script>
<style type="text/css">
body {
background: white;
Expand Down

0 comments on commit 54baf3c

Please sign in to comment.