handle selection and url extraction properly

This commit is contained in:
Dima Gerasimov 2019-01-04 22:59:34 +00:00
parent 4115b6e003
commit 5b871fed90
3 changed files with 39 additions and 9 deletions

View file

@ -16,6 +16,10 @@ function makeCaptureRequest(
'comment': comment, 'comment': comment,
}); });
if (url == null) {
showNotification('ERROR: trying to capture null');
return;
}
var request = new XMLHttpRequest(); var request = new XMLHttpRequest();
console.log(`[background] capturing ${data}`); console.log(`[background] capturing ${data}`);
@ -33,17 +37,45 @@ function makeCaptureRequest(
} else { } else {
// TODO more error context? // TODO more error context?
console.log(`[background] ERROR: ${request.status}`); 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... // TODO crap, doesn't really seem to respect urgency...
} }
}; };
request.onerror = () => {
console.log(request);
};
request.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
request.send(data); request.send(data);
} }
chrome.runtime.onMessage.addListener((message: any, sender: chrome$MessageSender, sendResponse) => {
if (message.method === CAPTURE_SELECTED_METHOD) { // TODO mm. not sure if should do browserAction.onclicked instead?? I suppose it's more flexible??
makeCaptureRequest(message.url); // 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
);
});
}); });

View file

@ -4,11 +4,9 @@ import {CAPTURE_SELECTED_METHOD} from './common';
function captureSelected () { function captureSelected () {
// TODO add command to run capture server as a hint?? // TODO add command to run capture server as a hint??
const url = "TODO FIXME"; console.log('[popup] capturing!');
console.log(`[popup] capturing ${url}`);
chrome.runtime.sendMessage({ chrome.runtime.sendMessage({
'method': CAPTURE_SELECTED_METHOD, 'method': CAPTURE_SELECTED_METHOD,
'url': url,
}, resp => { }, resp => {
console.log("[popup] captured!"); console.log("[popup] captured!");
}); });

View file

@ -5,10 +5,10 @@
"page": "background.html" "page": "background.html"
}, },
"browser_action": { "browser_action": {
"default_popup": "popup.html"
}, },
"permissions": [ "permissions": [
"notifications", "notifications",
"activeTab",
"http://*/" "http://*/"
], ],
"commands": { "commands": {