From 1d8a15acf6d11b1ceb0fa94f6a11ed2a7686ba11 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Fri, 4 Jan 2019 23:57:34 +0000 Subject: [PATCH] Add title capture --- server | 8 ++++++++ server.py | 13 ++++++++++--- src/js/background.js | 44 ++++++++++++++++++++++++-------------------- src/js/config.js | 2 +- 4 files changed, 43 insertions(+), 24 deletions(-) create mode 100755 server diff --git a/server b/server new file mode 100755 index 0000000..c2adf5d --- /dev/null +++ b/server @@ -0,0 +1,8 @@ +#!/bin/bash +set -eux + +cd "$(dirname "$0")" + +PORT=8090 + +hug -p "$PORT" -f server.py >server.log 2>server.err diff --git a/server.py b/server.py index 7a45c5b..9232572 100644 --- a/server.py +++ b/server.py @@ -20,17 +20,24 @@ def log(*things): @hug.post('/capture') def capture( url: T.text, + title: T.Nullable(T.text), selection: T.Nullable(T.text), comment: T.Nullable(T.text), ): - log("capturing", url, selection, comment) + log("capturing", url, title, selection, comment) + heading = url parts = [] + + if not empty(title): + heading = title + parts.append(url) + # TODO not sure, maybe add as org quote? if not empty(selection): parts.extend([ 'Selection:', - selection, + selection, # TODO tabulate? ]) if not empty(comment): parts.extend([ @@ -46,7 +53,7 @@ def capture( try: append_org_entry( CAPTURE_PATH, - heading=url, + heading=heading, body=body, tags=['grasp'], todo=False, diff --git a/src/js/background.js b/src/js/background.js index e8cdeef..7eaf8e2 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -4,22 +4,19 @@ import {CAPTURE_SELECTED_METHOD, showNotification} from './common'; import {capture_url} from './config'; -function makeCaptureRequest( - url: string, - selection: ?string=null, - comment: ?string=null, - // TODO anything alse?? -) { - if (url == null) { - showNotification('ERROR: trying to capture null'); - return; - } - const data = JSON.stringify({ - 'url': url, - 'selection': selection, - 'comment': comment, - }); +type Params = { + url: string, + title: ?string, + selection: ?string, + comment: ?string, +} + + +function makeCaptureRequest( + params: Params, +) { + const data = JSON.stringify(params); console.log(`[background] capturing ${data}`); @@ -66,7 +63,12 @@ function makeCaptureRequest( // TODO handle cannot access chrome:// url?? // TODO ok, need to add comment popup? chrome.browserAction.onClicked.addListener(tab => { - const url = tab.url; + if (tab.url == null) { + showNotification('ERROR: trying to capture null'); + return; + } + const url: string = tab.url; + const title: ?string = tab.title; // console.log('action!'); // ugh.. https://stackoverflow.com/a/19165930/706389 @@ -74,9 +76,11 @@ chrome.browserAction.onClicked.addListener(tab => { code: "window.getSelection().toString();" }, selections => { const selection = selections == null ? null : selections[0]; - makeCaptureRequest( - url, - selection - ); + makeCaptureRequest({ + url: url, + title: title, + selection: selection, + comment: null, + }); }); }); diff --git a/src/js/config.js b/src/js/config.js index 88bc57a..f6da0bb 100644 --- a/src/js/config.js +++ b/src/js/config.js @@ -2,7 +2,7 @@ // TODO configure port in options? const capture_endpoint = 'capture'; -const port = 8000; +const port = 8090; export function capture_url (): string { return `http://localhost:${port}/${capture_endpoint}`;