From 9d7bc76ffd7786d6c34a95a6b31425f981eecd54 Mon Sep 17 00:00:00 2001 From: sienori Date: Fri, 22 Feb 2019 01:56:34 +0900 Subject: [PATCH] Replace background with ES6 --- src/background/background.js | 152 +++++--------------------- src/background/menus.js | 103 +++++++++++++++++ src/background/onInstalledListener.js | 25 +++++ src/common/genelateLangOptions.js | 1 + src/settings/defaultSettings.js | 1 + 5 files changed, 157 insertions(+), 125 deletions(-) create mode 100644 src/background/menus.js create mode 100644 src/background/onInstalledListener.js diff --git a/src/background/background.js b/src/background/background.js index 665a95d..fbbcb09 100644 --- a/src/background/background.js +++ b/src/background/background.js @@ -1,130 +1,32 @@ -/* Copyright (c) 2017-2018 Sienori All rights reserved. - * 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/. */ +import browser from "webextension-polyfill"; +import browserInfo from "browser-info"; +import log from "loglevel"; +import { initSettings, handleSettingsChange } from "src/settings/settings"; +import { updateLogLevel, overWriteLogLevel } from "src/common/log"; +import onInstalledListener from "./onInstalledListener"; +import { showMenus, onMenusShownListener, onMenusClickedListener } from "./menus"; -//初回起動時にオプションページを表示して設定を初期化 -browser.runtime.onInstalled.addListener(details => { - if (details.reason != "install" && details.reason != "update") return; +const logDir = "background/background"; - browser.tabs.create({ - url: "options/options.html#information?action=updated", - active: false +const addListeners = () => { + browser.storage.onChanged.addListener((changes, areaName) => { + handleSettingsChange(changes, areaName); + updateLogLevel(); + showMenus(); }); -}); + const isValidMenusOnShown = browserInfo().name === "Firefox" && browserInfo().version >= 60; + if (isValidMenusOnShown) browser.contextMenus.onShown.addListener(onMenusShownListener); + browser.contextMenus.onClicked.addListener(onMenusClickedListener); +}; -let S = new settingsObj(); +const init = async () => { + await initSettings(); + overWriteLogLevel(); + updateLogLevel(); + log.info(logDir, "init()"); + addListeners(); + showMenus(); +}; +init(); -browser.storage.onChanged.addListener(showMenu); -if (typeof browser.contextMenus.onShown != "undefined") - browser.contextMenus.onShown.addListener(updateMenu); - -S.init().then(function() { - showMenu(); -}); - -function showMenu() { - if (S.get().ifShowMenu) { - menuRemove(); - menuCreate(); - } else menuRemove(); -} - -//テキストまたはリンクの選択時はページ翻訳を非表示にする -function updateMenu(info, tab) { - if (info.contexts.includes("selection") || info.contexts.includes("link")) { - browser.contextMenus.update("translatePage", { contexts: ["password"] }); //passwordにすることで事実上無効にする - } else { - browser.contextMenus.update("translatePage", { contexts: ["all"] }); - } - browser.contextMenus.refresh(); -} - -//メニューを表示 -function menuCreate() { - browser.contextMenus.create({ - id: "translatePageOnTab", - title: browser.i18n.getMessage("translatePageMenu"), - contexts: ["tab"] - }); - - browser.contextMenus.create({ - id: "translatePage", - title: browser.i18n.getMessage("translatePageMenu"), - contexts: ["all"] - }); - - browser.contextMenus.create({ - id: "translateText", - title: browser.i18n.getMessage("translateTextMenu"), - contexts: ["selection"] - }); - - browser.contextMenus.create({ - id: "translateLink", - title: browser.i18n.getMessage("translateLinkMenu"), - contexts: ["link"] - }); -} - -//メニューを削除 -function menuRemove() { - browser.contextMenus.removeAll(); -} - -//メニュークリック時 -browser.contextMenus.onClicked.addListener(function(info, tab) { - switch (info.menuItemId) { - case "translatePage": - case "translatePageOnTab": - translatePageMenu(info, tab); - break; - case "translateText": - translateTextMenu(info, tab); - break; - case "translateLink": - translateLinkMenu(info, tab); - break; - } -}); - -//テキストを翻訳 -function translateTextMenu(info, tab) { - browser.tabs.sendMessage(tab.id, { - message: "showPanelFromMenu" - }); -} - -//ページ全体を翻訳 -function translatePageMenu(info, tab) { - browser.tabs.create({ - url: - "https://translate.google.com/translate?hl=" + - S.get().targetLang + - "&sl=auto&u=" + - encodeURIComponent(info.pageUrl), - active: true, - index: tab.index + 1 - }); -} - -//リンクを翻訳 -function translateLinkMenu(info, tab) { - browser.tabs.create({ - url: - "https://translate.google.com/translate?hl=" + - S.get().targetLang + - "&sl=auto&u=" + - encodeURIComponent(info.linkUrl), - active: true, - index: tab.index + 1 - }); -} - -//スクリプトからのメッセージに返信 -browser.runtime.onMessage.addListener(function(request) { - switch (request.message) { - case "getSetting": - break; - } -}); +browser.runtime.onInstalled.addListener(onInstalledListener); diff --git a/src/background/menus.js b/src/background/menus.js new file mode 100644 index 0000000..f2c4e9a --- /dev/null +++ b/src/background/menus.js @@ -0,0 +1,103 @@ +import browser from "webextension-polyfill"; +import browserInfo from "browser-info"; +import log from "loglevel"; +import { getSettings } from "src/settings/settings"; + +const logDir = "background/menus"; + +export const showMenus = () => { + if (getSettings("ifShowMenu")) { + removeMenus(); + createMenus(); + } else removeMenus(); +}; + +export const onMenusShownListener = (info, tab) => { + //テキストまたはリンクの選択時はページ翻訳を非表示にする + if (info.contexts.includes("selection") || info.contexts.includes("link")) { + //passwordにすることで事実上無効にする + browser.contextMenus.update("translatePage", { contexts: ["password"] }); + } else { + browser.contextMenus.update("translatePage", { contexts: ["all"] }); + } + browser.contextMenus.refresh(); +}; + +export const onMenusClickedListener = (info, tab) => { + log.log(logDir, "onMenusClickedListener()", info, tab); + switch (info.menuItemId) { + case "translatePage": + case "translatePageOnTab": + translatePage(info, tab); + break; + case "translateText": + translateText(tab); + break; + case "translateLink": + translateLink(info, tab); + break; + } +}; + +function createMenus() { + const isValidContextsTypeTab = browserInfo().name === "Firefox" && browserInfo().version >= 53; + if (isValidContextsTypeTab) { + browser.contextMenus.create({ + id: "translatePageOnTab", + title: browser.i18n.getMessage("translatePageMenu"), + contexts: ["tab"] + }); + } + + browser.contextMenus.create({ + id: "translatePage", + title: browser.i18n.getMessage("translatePageMenu"), + contexts: ["all"] + }); + + browser.contextMenus.create({ + id: "translateText", + title: browser.i18n.getMessage("translateTextMenu"), + contexts: ["selection"] + }); + + browser.contextMenus.create({ + id: "translateLink", + title: browser.i18n.getMessage("translateLinkMenu"), + contexts: ["link"] + }); +} + +function removeMenus() { + browser.contextMenus.removeAll(); +} + +function translateText(tab) { + browser.tabs.sendMessage(tab.id, { + message: "translateSelectedText" + }); +} + +function translatePage(info, tab) { + const targetLang = getSettings("targetLang"); + const encodedPageUrl = encodeURIComponent(info.pageUrl); + const translationUrl = `https://translate.google.com/translate?hl=${targetLang}&sl=auto&u=${encodedPageUrl}`; + + browser.tabs.create({ + url: translationUrl, + active: true, + index: tab.index + 1 + }); +} + +function translateLink(info, tab) { + const targetLang = getSettings("targetLang"); + const encodedLinkUrl = encodeURIComponent(info.linkUrl); + const translationUrl = `https://translate.google.com/translate?hl=${targetLang}&sl=auto&u=${encodedLinkUrl}`; + + browser.tabs.create({ + url: translationUrl, + active: true, + index: tab.index + 1 + }); +} diff --git a/src/background/onInstalledListener.js b/src/background/onInstalledListener.js new file mode 100644 index 0000000..0b0f6b1 --- /dev/null +++ b/src/background/onInstalledListener.js @@ -0,0 +1,25 @@ +import browser from "webextension-polyfill"; +import log from "loglevel"; +import { initSettings, getSettings, setSettings } from "src/settings/settings"; +import { initShortcuts } from "./keyboardShortcuts"; + +const logDir = "background/onInstalledListener"; + +const openOptionsPage = active => { + browser.tabs.create({ + url: "options/index.html#information?action=updated", + active: active + }); +}; + +export default async details => { + if (details.reason != "install" && details.reason != "update") return; + log.info(logDir, "onInstalledListener()", details); + + await initSettings(); + initShortcuts(); + + const isShowOptionsPage = getSettings("isShowOptionsPageWhenUpdated"); + if (isShowOptionsPage) openOptionsPage(false); + setSettings("isShowUpdated", true); +}; diff --git a/src/common/genelateLangOptions.js b/src/common/genelateLangOptions.js index 3fba170..b398b06 100644 --- a/src/common/genelateLangOptions.js +++ b/src/common/genelateLangOptions.js @@ -1,3 +1,4 @@ +import browser from "webextension-polyfill"; const alphabeticallySort = (a, b) => a.name.localeCompare(b.name); export default () => { diff --git a/src/settings/defaultSettings.js b/src/settings/defaultSettings.js index a13a622..b764567 100644 --- a/src/settings/defaultSettings.js +++ b/src/settings/defaultSettings.js @@ -1,3 +1,4 @@ +import browser from "webextension-polyfill"; import genelateLangOptions from "src/common/genelateLangOptions"; const getDefaultLangs = () => {