From c1e46e36e1f2e3e508eaf4df413dc86f193e6d4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20Norman=20Hauk=C3=A5s?= Date: Sun, 14 Aug 2016 20:01:44 +0200 Subject: [PATCH] Use api for requesting user info Found an api for requesting the email of the logged in user. Let's see how that works. Also discovered that content scripts cannot login a user that has to be done by a background script. --- src/app/menu/settings.controller.ts | 20 ++++++++++++++------ src/plugin-specific/background.js | 28 +++++++++++++++++++++++++--- src/plugin-specific/manifest.json | 2 +- 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/app/menu/settings.controller.ts b/src/app/menu/settings.controller.ts index b4a0f93..8f4ec9b 100644 --- a/src/app/menu/settings.controller.ts +++ b/src/app/menu/settings.controller.ts @@ -31,7 +31,7 @@ export class SettingsCtrl { $scope.vm = this; this.loadSettings() - + this.startListeningForLogins() } loadSettings() { @@ -67,11 +67,19 @@ export class SettingsCtrl { } testLogin() { - debugger; - chrome.identity.getAuthToken({'interactive': true}, - (token) => { - debugger; - }) + if (typeof chrome === 'undefined') return; //do nothing + chrome.runtime.sendMessage({command: 'requestUserInfo'}) + } + + startListeningForLogins() { + chrome.runtime.onMessage.addListener( + (request, sender, sendResponse) => { + if (request.loginObj) { + this.$log.debug('listenForLogin() got a message from the extension') + this.$log.debug(request) + } + } + ); } } \ No newline at end of file diff --git a/src/plugin-specific/background.js b/src/plugin-specific/background.js index bb2a487..e773b98 100644 --- a/src/plugin-specific/background.js +++ b/src/plugin-specific/background.js @@ -12,9 +12,7 @@ chrome.browserAction.onClicked.addListener(function (tab) { }); function isMenuOpen(callback) { - chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) { - chrome.tabs.sendMessage(tabs[0].id, "isMenuOpen", callback); - }); + messageExtension('isMenuOpen', callback); } function injectIframe(tab) { @@ -27,4 +25,28 @@ function injectIframe(tab) { allFrames: true }); }); +} + +/** + * Start listening for messages coming from the injected + * javascript. + */ +chrome.runtime.onMessage.addListener((msg) => { + if (msg.command === 'requestUserInfo') { + console.log('Extension got a command to retrieve user info'); + chrome.identity.getProfileUserInfo((userInfo) => { + console.log(userInfo) + messageExtension({loginObj: userInfo}) + }); + } +}); + +function messageExtension(messageToSend, callback) { + chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) { + if (callback) { + chrome.tabs.sendMessage(tabs[0].id, messageToSend, callback); + } else { + chrome.tabs.sendMessage(tabs[0].id, messageToSend); + } + }); } \ No newline at end of file diff --git a/src/plugin-specific/manifest.json b/src/plugin-specific/manifest.json index e7c7e00..2ba9ff7 100644 --- a/src/plugin-specific/manifest.json +++ b/src/plugin-specific/manifest.json @@ -1,7 +1,7 @@ { "name": "Tag you're it", "description": "A browser tool for tagging words with exact definitions.", - "version": "1.0.4", + "version": "1.0.5", "manifest_version": 2, "icons": { "16": "icon16.png",