From 5b871fed907c9271133975a1517a39b71080710b Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Fri, 4 Jan 2019 22:59:34 +0000 Subject: [PATCH] handle selection and url extraction properly --- src/js/background.js | 42 +++++++++++++++++++++++++++++++++++++----- src/js/popup.js | 4 +--- src/manifest.json | 2 +- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/js/background.js b/src/js/background.js index 7ebfd87..8a8f53d 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -16,6 +16,10 @@ function makeCaptureRequest( 'comment': comment, }); + if (url == null) { + showNotification('ERROR: trying to capture null'); + return; + } var request = new XMLHttpRequest(); console.log(`[background] capturing ${data}`); @@ -33,17 +37,45 @@ function makeCaptureRequest( } else { // TODO more error context? console.log(`[background] ERROR: ${request.status}`); - showNotification(`ERROR: ${request.status}`, 1); + + var explain = ""; + if (request.status === 0) { + explain = `Unavaiable: ${capture_url()}`; + } + showNotification(`ERROR: ${request.status}: ${explain}`, 1); // TODO crap, doesn't really seem to respect urgency... } }; + request.onerror = () => { + console.log(request); + }; request.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); request.send(data); } -chrome.runtime.onMessage.addListener((message: any, sender: chrome$MessageSender, sendResponse) => { - if (message.method === CAPTURE_SELECTED_METHOD) { - makeCaptureRequest(message.url); - } + +// TODO mm. not sure if should do browserAction.onclicked instead?? I suppose it's more flexible?? +// TODO hmm. content script or js?? +// chrome.runtime.onMessage.addListener((message: any, sender: chrome$MessageSender, sendResponse) => { +// if (message.method === CAPTURE_SELECTED_METHOD) { +// } +// }); + +// TODO handle cannot access chrome:// url?? +// TODO ok, need to add comment popup? +chrome.browserAction.onClicked.addListener(tab => { + const url = tab.url; + + // console.log('action!'); + // ugh.. https://stackoverflow.com/a/19165930/706389 + chrome.tabs.executeScript( { + code: "window.getSelection().toString();" + }, selections => { + const selection = selections == null ? null : selections[0]; + makeCaptureRequest( + url, + selection + ); + }); }); diff --git a/src/js/popup.js b/src/js/popup.js index c7b2fbe..c24d54a 100644 --- a/src/js/popup.js +++ b/src/js/popup.js @@ -4,11 +4,9 @@ import {CAPTURE_SELECTED_METHOD} from './common'; function captureSelected () { // TODO add command to run capture server as a hint?? - const url = "TODO FIXME"; - console.log(`[popup] capturing ${url}`); + console.log('[popup] capturing!'); chrome.runtime.sendMessage({ 'method': CAPTURE_SELECTED_METHOD, - 'url': url, }, resp => { console.log("[popup] captured!"); }); diff --git a/src/manifest.json b/src/manifest.json index 8eaff3b..40ae87a 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -5,10 +5,10 @@ "page": "background.html" }, "browser_action": { - "default_popup": "popup.html" }, "permissions": [ "notifications", + "activeTab", "http://*/" ], "commands": {