From 02707d0d0446043770aa9b3090557e6fd6fe0454 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20Norman=20Hauk=C3=A5s?= Date: Sun, 10 Jan 2016 20:15:34 +0100 Subject: [PATCH] No longer using popup.html. Clicking the plugin button will now toggle the menu directly. --- src/index.ts | 26 +++++-- src/plugin-specific/add-iframe-to-page.js | 5 ++ src/plugin-specific/background.js | 60 +++++++++++++++ src/plugin-specific/manifest.json | 4 +- src/plugin-specific/popup.html | 40 ---------- src/plugin-specific/popup.js | 92 ----------------------- 6 files changed, 87 insertions(+), 140 deletions(-) delete mode 100644 src/plugin-specific/popup.html delete mode 100644 src/plugin-specific/popup.js diff --git a/src/index.ts b/src/index.ts index 12fb19f..bbd51bd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,12 +16,28 @@ module tagIt { .service('TagStorageService', TagStorageService) .controller('MenuCtrl', MenuCtrl); - export function init (callback: () => void) { - angular.bootstrap( - document.getElementById("tagit-iframe") - .contentDocument.getElementById("tagit-menu") - , ['tagit']); + export function init(callback: () => void) { + var iframe = document.getElementById("tagit-iframe") + angular.bootstrap(iframe.contentDocument.getElementById("tagit-menu"), ['tagit']); console.log('TagIt menu loaded'); + setupChromeListener(); + } + + function setupChromeListener() { + if (typeof chrome === 'undefined') return; //do nothing + + chrome.runtime.onMessage.addListener( + function(request, sender, sendResponse) { + console.log(sender.tab ? + "from a content script:" + sender.tab.url : + "from the extension"); + if (request.greeting == "hello") { + sendResponse({ farewell: "goodbye" }); + } else if (request === 'isMenuOpen') { + sendResponse(true); + } + } + ); } // export function init (callback: () => void) { diff --git a/src/plugin-specific/add-iframe-to-page.js b/src/plugin-specific/add-iframe-to-page.js index 214c58a..e1981d4 100644 --- a/src/plugin-specific/add-iframe-to-page.js +++ b/src/plugin-specific/add-iframe-to-page.js @@ -1,3 +1,8 @@ + +/* + This javascript is injected to page by background.js. +*/ + $.get(chrome.extension.getURL('index-angular-app.html'), function (htmlData) { var iframe = document.createElement('iframe'); iframe.id = 'tagit-iframe'; diff --git a/src/plugin-specific/background.js b/src/plugin-specific/background.js index e69de29..23267b0 100644 --- a/src/plugin-specific/background.js +++ b/src/plugin-specific/background.js @@ -0,0 +1,60 @@ + +chrome.browserAction.onClicked.addListener(function () { + isMenuOpen(function (responseIsItOpen) { + if (responseIsItOpen) { + console.log('Closing menu'); + chrome.tabs.reload(); + } else { + console.log('Opening menu'); + injectIframe(); + } + }) +}); + +function isMenuOpen(callback) { + chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) { + chrome.tabs.sendMessage(tabs[0].id, "isMenuOpen" , callback); + }); +} + +function injectIframe() { + loadJquery(function () { + chrome.tabs.executeScript(null, { + file: 'add-iframe-to-page.js' + }, loadPluginDeps); + }); + + function loadJquery(callback) { + chrome.tabs.executeScript(null, { + file: 'vendor/jquery/dist/jquery.js', + allFrames: true + }, callback); + } + + function loadPluginDeps() { + chrome.tabs.executeScript(null, { + file: 'vendor/vendor.js', + allFrames: true + }, loadPlugin); + } + + function loadPlugin() { + chrome.tabs.executeScript(null, { + file: 'bundle.js', + allFrames: true + }, loadCss); + } + + function loadCss() { + chrome.tabs.insertCSS(null, { + file: 'style.css', + allFrames: true + }, initPlugin); + } + + function initPlugin() { + chrome.tabs.executeScript(null, { + code: 'tagIt.init();' + }); + } +} \ No newline at end of file diff --git a/src/plugin-specific/manifest.json b/src/plugin-specific/manifest.json index 2158bd1..0e743fe 100644 --- a/src/plugin-specific/manifest.json +++ b/src/plugin-specific/manifest.json @@ -24,8 +24,6 @@ "bundle.js" ], "browser_action": { - "default_title": "Tag you're it! Click to enable or disable tagging of web content.", - "default_icon": "icon48.png", - "default_popup": "popup.html" + "default_icon": "icon48.png" } } diff --git a/src/plugin-specific/popup.html b/src/plugin-specific/popup.html deleted file mode 100644 index b3ce4f7..0000000 --- a/src/plugin-specific/popup.html +++ /dev/null @@ -1,40 +0,0 @@ - - - - Popup menu for TagIt plugin - - - - - - - - -
-

Tag you're it

-
- -
-

-
-
- - -
-
- - -
-
- - -
-
-
- - - \ No newline at end of file diff --git a/src/plugin-specific/popup.js b/src/plugin-specific/popup.js deleted file mode 100644 index 91e3c5e..0000000 --- a/src/plugin-specific/popup.js +++ /dev/null @@ -1,92 +0,0 @@ - -document.addEventListener("DOMContentLoaded", function(event) { - document.getElementById('js-open-menu').addEventListener('click', openMenu); -}); - -function openMenu () { - logToBG('open menu was clicked'); - injectIframe(); -} - -function injectIframe () { - loadJquery(function () { - logToBG('jquery was loaded'); - chrome.tabs.executeScript(null, { - file: 'add-iframe-to-page.js' - }, loadPluginDeps); - }); - - function loadPluginDeps () { - chrome.tabs.executeScript(null, { - file: 'vendor/vendor.js', - allFrames: true - }, loadPlugin); - } - - function loadPlugin () { - chrome.tabs.executeScript(null, { - file: 'bundle.js', - allFrames: true - }, loadCss); - } - - function loadCss () { - chrome.tabs.insertCSS(null, { - file: 'style.css', - allFrames: true - }, initPlugin); - } - - function initPlugin () { - chrome.tabs.executeScript(null, { - code: 'tagIt.init();' - }); - } - - function loadJquery (callback) { - chrome.tabs.executeScript(null, { - file: 'vendor/jquery/dist/jquery.js', - allFrames: true - }, callback); - } -} - -function injectScripts () { - logToBG('injectAngular'); - // Prevent immediate automatic bootstrapping - chrome.tabs.executeScript(null, { - code: 'window.name = "NG_DEFER_BOOTSTRAP!" + window.name;' - }, loadPluginDeps); - - function loadPluginDeps () { - chrome.tabs.executeScript(null, { - file: 'vendor/vendor.js' - }, loadPlugin); - } - - function loadPlugin () { - chrome.tabs.executeScript(null, { - file: 'bundle.js' - }, loadCss); - } - - function loadCss () { - chrome.tabs.insertCSS(null, {file: 'vendor/bootstrap/dist/css/bootstrap.min.css'}, - chrome.tabs.insertCSS(null, {file: 'style.css'}, bootstrapAngularMenu)) - } - - function bootstrapAngularMenu () { - chrome.tabs.executeScript(null, { - code: 'tagIt.init();' - }); - } - -} - -function addScript (relativeScriptPath, callback) { - chrome.tabs.executeScript(null, {file: relativeScriptPath}, callback); -} - -function logToBG (msg) { - chrome.runtime.sendMessage({logMsg: msg}); -} \ No newline at end of file