From ee3053fa5c9526f28a1fe53a9c0da9d656a047aa Mon Sep 17 00:00:00 2001 From: Andrei Cristian Petcu Date: Sat, 13 May 2017 17:25:26 +0300 Subject: [PATCH] added page action that actually works --- manifest.json | 2 +- scripts/background.js | 28 ++++++++++++++++++++++++---- updates.json | 4 +++- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/manifest.json b/manifest.json index a7d6b42..e1a5bd8 100644 --- a/manifest.json +++ b/manifest.json @@ -1,6 +1,6 @@ { "name": "__MSG_appName__", - "version": "1.1.0", + "version": "1.2.0", "homepage_url": "https://github.com/andreicristianpetcu/google_translate_this", "manifest_version": 2, "description": "__MSG_appDescription__", diff --git a/scripts/background.js b/scripts/background.js index d916c49..3bdddef 100644 --- a/scripts/background.js +++ b/scripts/background.js @@ -1,5 +1,19 @@ 'use strict'; +const APPLICABLE_PROTOCOLS = ["http:", "https:"]; + +function protocolIsApplicable(url) { + var anchor = document.createElement('a'); + anchor.href = url; + return APPLICABLE_PROTOCOLS.includes(anchor.protocol); +} + +function showPageActionOnTab(tabInfo){ + if (protocolIsApplicable(tabInfo.url)) { + browser.pageAction.show(tabInfo.id); + } +} + function translateCurrentPage() { chrome.tabs.query({ currentWindow: true, @@ -25,14 +39,20 @@ browser.contextMenus.onClicked.addListener(function(info, tab) { }); browser.browserAction.onClicked.addListener(translateCurrentPage); - -chrome.contextMenus.create({ +browser.contextMenus.create({ id: "translate-current-page", title: "Translate Current Page", contexts: ["all"] }); browser.pageAction.onClicked.addListener(translateCurrentPage); -browser.tabs.onActivated.addListener(function(tabInfo){ - browser.pageAction.show(tabInfo.tabId); +browser.tabs.query({}).then((tabs) => { + var tab; + for (tab of tabs) { + showPageActionOnTab(tab); + } +}); + +browser.tabs.onUpdated.addListener((id, changeInfo, tab) => { + showPageActionOnTab(tab); }); diff --git a/updates.json b/updates.json index c7e9c5a..d657fe1 100644 --- a/updates.json +++ b/updates.json @@ -7,7 +7,9 @@ { "version": "1.0.2", "update_link": "https://github.com/andreicristianpetcu/google_translate_this/releases/download/v1.0.2/google_translate_this_page-1.0.2-an.fx.xpi" }, { "version": "1.0.3", - "update_link": "https://github.com/andreicristianpetcu/google_translate_this/releases/download/v1.0.3/google_translate_this_page-1.0.3-an.fx.xpi" } + "update_link": "https://github.com/andreicristianpetcu/google_translate_this/releases/download/v1.0.3/google_translate_this_page-1.0.3-an.fx.xpi" }, + { "version": "1.2.0", + "update_link": "https://github.com/andreicristianpetcu/google_translate_this/releases/download/v1.2.0/google_translate_this_page-1.2.0-an.fx.xpi" } ] } }