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,
});
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
);
});
});

View file

@ -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!");
});

View file

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