Skip to content

Commit

Permalink
added clipboard polling from server, no need clipboard inserter plugi…
Browse files Browse the repository at this point in the history
…n now
  • Loading branch information
Niakr1s committed Sep 24, 2022
1 parent 8acee0e commit fd067a8
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 41 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
# Needed software

- [Textractor](https://github.com/Artikash/Textractor). Configure it to extract sentences into system clipboard.
- [Clipboard Inserter](https://chrome.google.com/webstore/detail/clipboard-inserter/deahejllghicakhplliloeheabddjajm) extension. Don't change default options, just don't forget to enable it every time you launch browser.

# Usage

Expand Down
55 changes: 32 additions & 23 deletions client/src/app/services/clipboard.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { DOCUMENT } from '@angular/common';
import { HttpClient } from '@angular/common/http';
import { Inject, Injectable } from '@angular/core';
import { Observable, Subject } from 'rxjs';

interface ClipboardResponse {
clipboard: string;
}

@Injectable({
providedIn: 'root',
})
Expand All @@ -12,32 +17,36 @@ export class ClipboardService {
return this.clipboardSubject.asObservable();
}

private observer: MutationObserver;
constructor(@Inject(DOCUMENT) document: Document) {
this.observer = this.makeMutationObserver();

const body = document.querySelector('body');
if (!body) {
throw new Error('body is null');
}
this.observer.observe(body, { childList: true });
private previousClipboardContents: string = "";

constructor(private http: HttpClient) {
http.get<ClipboardResponse>("api/clipboard").toPromise()
.then((r) => {
this.setNewClipboardContents(r.clipboard);
this.startClipboardPoll();
})
.catch((e) => {
console.error(e);
})
}

private makeMutationObserver(): MutationObserver {
return new MutationObserver(() => {
const ps = document.getElementsByTagName('p');

// removing all except last
while (ps.length > 0) {
const sentence = ps[0].textContent;
ps[0].remove();
private startClipboardPoll() {
this.http.get<ClipboardResponse>("api/clipboardPoll").toPromise()
.then((r) => {
this.setNewClipboardContents(r.clipboard);
})
.catch((e) => {
console.error(e);
})
.finally(() => {
this.startClipboardPoll();
})
}

if (!sentence) {
continue;
}
private setNewClipboardContents(contents: string) {
if (!contents || contents === this.previousClipboardContents) return;

this.clipboardSubject.next(sentence);
}
});
this.previousClipboardContents = contents;
this.clipboardSubject.next(contents);
}
}
21 changes: 8 additions & 13 deletions client/src/app/services/sentence.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,14 @@ export class SentenceService {
Translation.createPending(translatorName, to)
);

if (_doActualTranslate) {
this.actualTranslate(translatorName, sentence, to)
.then((translation) => {
this.setTranslation(translatorName, sentence.id, translation);
})
.catch((e) => {
console.error(e);
});
}
this.actualTranslate(translatorName, sentence, to)
.then((translation) => {
console.log("got translation: ", translation);
this.setTranslation(translatorName, sentence.id, translation);
})
.catch((e) => {
console.error(e);
});
}

private async actualTranslate(
Expand Down Expand Up @@ -224,10 +223,6 @@ export class SentenceService {

sentence.setTranslation(translatorName, translation);
this.translationsUpdatedSubject.next(sentence);

if (this.isCurrent(sentence)) {
this.currentSencenceSubject.next(sentence);
}
}

private isCurrent(sentence: Sentence): boolean {
Expand Down
2 changes: 1 addition & 1 deletion client/src/app/translation/translation.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class TranslationComponent implements OnInit, OnDestroy {
}

updateTranslation(translatorName: string, sentence: Sentence, to: string) {
this.sentenceService.translate(translatorName, sentence, to);
this.sentenceService.translate(translatorName, sentence, to, true);
}

ngOnDestroy(): void {
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ go 1.16
require (
github.com/chromedp/chromedp v0.6.10
github.com/stretchr/testify v1.7.0
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 // indirect
github.com/tjgq/broadcast v0.0.0-20140913210940-9d7fb01523c6 // indirect
golang.design/x/clipboard v0.6.2
)
43 changes: 43 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/chromedp/cdproto v0.0.0-20210323015217-0942afbea50e h1:UimnzLuARNkGi2XsNznUoOLFP/noktdUMrr7fcb3D4U=
github.com/chromedp/cdproto v0.0.0-20210323015217-0942afbea50e/go.mod h1:At5TxYYdxkbQL0TSefRjhLE3Q0lgvqKKMSFUglJ7i1U=
github.com/chromedp/chromedp v0.6.10 h1:Yd4X6ngkWbn6A+hv6mUzV9kVHrPn7L4+vf2uyNbze2s=
Expand All @@ -21,9 +22,51 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tjgq/broadcast v0.0.0-20140913210940-9d7fb01523c6 h1:VMtA75kCqdmPOl4paSS21sFEyHe3XvNk4DyGk9ShTdY=
github.com/tjgq/broadcast v0.0.0-20140913210940-9d7fb01523c6/go.mod h1:WpoK/yqwe/aERxbD6Prs2+JQ24FkcC0I1KXqhTlBdd0=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.design/x/clipboard v0.6.2 h1:a3Np4qfKnLWwfFJQhUWU3IDeRfmVuqWl+QPtP4CSYGw=
golang.design/x/clipboard v0.6.2/go.mod h1:kqBSweBP0/im4SZGGjLrppH0D400Hnfo5WbFKSNK8N4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 h1:estk1glOnSVeJ9tdEZZc5mAMDZk5lNJNyJ6DvrBkTEU=
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56/go.mod h1:JhuoJpWY28nO4Vef9tZUw9qufEGTyX1+7lmHxV5q5G4=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.0.0-20211028202545-6944b10bf410 h1:hTftEOvwiOq2+O8k2D5/Q7COC7k5Qcrgc2TFURJYnvQ=
golang.org/x/image v0.0.0-20211028202545-6944b10bf410/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/mobile v0.0.0-20210716004757-34ab1303b554 h1:3In5TnfvnuXTF/uflgpYxSCEGP2NdYT37KsPh3VjZYU=
golang.org/x/mobile v0.0.0-20210716004757-34ab1303b554/go.mod h1:jFTmtFYCV0MFtXBU+J5V/+5AUeVS0ON/0WkE/KSrl6E=
golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 h1:F5Gozwx4I1xtr/sr/8CFbb57iKi3297KFs0QDbGN60A=
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
Expand Down
64 changes: 64 additions & 0 deletions src/server/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"context"
"embed"
"encoding/json"
"fmt"
Expand All @@ -9,7 +10,11 @@ import (
"mime"
"net/http"
"net/url"
"time"
"vnr/src/translator"

"github.com/tjgq/broadcast"
"golang.design/x/clipboard"
)

//go:embed static
Expand All @@ -26,6 +31,10 @@ type ServerOptions struct {
Translators map[string]Translator
}

type ClipboardPollHandlerOptions struct {
Delay time.Duration
}

func StartServer(options ServerOptions) {
mime.AddExtensionType(".js", "application/javascript")

Expand All @@ -46,6 +55,16 @@ func StartServer(options ServerOptions) {
http.HandleFunc(fmt.Sprintf("/api/langs/%s", name), langsHandler(translator))
}

err = clipboard.Init()
if err != nil {
panic(err)
}

http.HandleFunc("/api/clipboard", clipboardHandler())
http.HandleFunc("/api/clipboardPoll", clipboardPollHandler(ClipboardPollHandlerOptions{
Delay: time.Second * 10,
}))

log.Printf("Listening on %s...", options.Port)
// start the server
err = http.ListenAndServe(options.Port, nil)
Expand Down Expand Up @@ -129,3 +148,48 @@ func translationOptionsFromQuery(query url.Values) translator.TranslationOptions
}
return translationOptions
}

func clipboardHandler() http.HandlerFunc {
type Response struct {
Clipboard string `json:"clipboard"`
}

return func(w http.ResponseWriter, r *http.Request) {
contents := clipboard.Read(clipboard.FmtText)
res := Response{
Clipboard: string(contents),
}
writeJson(res, w)
}
}

// Returns empty string, if clipboard wasn't changed
func clipboardPollHandler(options ClipboardPollHandlerOptions) http.HandlerFunc {
type Response struct {
Clipboard string `json:"clipboard"`
}
res := Response{}

clipboardBroadcaster := broadcast.New(0)
go func() {
clipboardCh := clipboard.Watch(context.TODO(), clipboard.FmtText)
for {
contents := <-clipboardCh
clipboardBroadcaster.Send(contents)
}
}()

return func(w http.ResponseWriter, r *http.Request) {
l := clipboardBroadcaster.Listen()
defer l.Close()

select {
case contents := <-l.Ch:
res.Clipboard = string(contents.([]byte))

case <-time.After(options.Delay):
}

writeJson(res, w)
}
}
2 changes: 1 addition & 1 deletion src/server/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<style>body{background-color:#202020;color:#bcbcbc}</style><link rel="stylesheet" href="styles.418e91d02b211094.css" media="print" onload="this.media='all'"><noscript><link rel="stylesheet" href="styles.418e91d02b211094.css"></noscript></head>
<body>
<app-root></app-root>
<script src="runtime.01fec06f14767966.js" type="module"></script><script src="polyfills.ad7e278c33bad79d.js" type="module"></script><script src="main.b41393e57a1536fa.js" type="module"></script>
<script src="runtime.01fec06f14767966.js" type="module"></script><script src="polyfills.ad7e278c33bad79d.js" type="module"></script><script src="main.b1369bccfa4daa1b.js" type="module"></script>

</body></html>

Large diffs are not rendered by default.

0 comments on commit fd067a8

Please sign in to comment.