1- async function screenshotElement ( element ) {
2- const canvas = await html2canvas ( element ) ;
3- canvas . toBlob ( blob => {
4- const url = URL . createObjectURL ( blob ) ;
5- const a = document . createElement ( 'a' ) ;
6- a . href = url ;
7- a . download = 'screenshot.png' ;
8- a . click ( ) ;
9- URL . revokeObjectURL ( url ) ;
10- } ) ;
11- }
12-
131( ( ) => {
142 const elForm = document . querySelector ( "form.search-form" ) ;
153 const elQ = document . querySelector ( "#q" ) ;
@@ -127,33 +115,6 @@ async function screenshotElement(element) {
127115 elQ . select ( ) ;
128116} ) ( ) ;
129117
130- // Screenshot sharing.
131- ( ( ) => {
132- document . querySelectorAll ( "a.export" ) . forEach ( ( el ) => {
133- el . onclick = async ( e ) => {
134- e . preventDefault ( ) ;
135- const guid = el . dataset . guid ;
136- const entryEl = document . querySelector ( `.entry[data-guid='${ guid } ']` ) ;
137- if ( ! entryEl ) {
138- alert ( "Could not find entry to export" ) ;
139- return ;
140- }
141-
142- const title = entryEl . dataset . head ;
143- // Make the filename by stripping spaces from the head word(s).
144- const filename = title . replace ( / \s + / g, "_" ) . toLowerCase ( ) ;
145-
146- try {
147- await shareDOM ( entryEl , `${ title } meaning` , `${ localStorage . from_lang } to ${ localStorage . to_lang } meaning\n\n${ window . location . href } ` , `${ filename } .png` ) ;
148- } catch ( err ) {
149- console . error ( "Error sharing entry:" , err ) ;
150- alert ( `Error sharing entry: ${ err ?. message || err } ` ) ;
151- }
152- } ;
153- } ) ;
154- } ) ( ) ;
155-
156-
157118// Submission form.
158119( ( ) => {
159120 function filterTypes ( e ) {
@@ -250,7 +211,7 @@ async function screenshotElement(element) {
250211
251212// Autocomplete.
252213( ( ) => {
253- if ( ! autocomp ) {
214+ if ( ! autocomp ) {
254215 return ;
255216 }
256217
@@ -265,13 +226,13 @@ async function screenshotElement(element) {
265226 clearTimeout ( debounce ) ;
266227 return new Promise ( resolve => {
267228 debounce = setTimeout ( async ( ) => {
268- const response = await fetch ( `${ _ROOT_URL } /api/autocomplete/${ langCode } /${ val } ` ) ;
269- const data = await response . json ( ) ;
229+ const response = await fetch ( `${ _ROOT_URL } /api/autocomplete/${ langCode } /${ val } ` ) ;
230+ const data = await response . json ( ) ;
270231
271- const suggestions = data . data . map ( item => item . content [ 0 ] ) ;
232+ const suggestions = data . data . map ( item => item . content [ 0 ] ) ;
272233
273- debounce = null ;
274- resolve ( suggestions ) ;
234+ debounce = null ;
235+ resolve ( suggestions ) ;
275236 } , 50 ) ;
276237 } ) ;
277238 } ,
@@ -287,3 +248,36 @@ async function screenshotElement(element) {
287248 }
288249 } ) ;
289250} ) ( ) ;
251+
252+ // Share link (.share-link) that invokes web share API.
253+ ( ( ) => {
254+ window . setTimeout ( ( ) => {
255+ // For some reason, page doesn't scroll to hash on load. Do it manually.
256+ if ( window . location . hash ) {
257+ document . querySelector ( window . location . hash ) ?. scrollIntoView ( ) ;
258+ }
259+ } , 100 ) ;
260+
261+ if ( ! navigator . share ) {
262+ return ;
263+ }
264+
265+ document . querySelectorAll ( "a[data-share-guid]" ) . forEach ( ( el ) => {
266+ el . onclick = async ( e ) => {
267+ e . preventDefault ( ) ;
268+
269+ const def = document . querySelector ( `#${ el . dataset . shareGuid } .def:first-child` ) ;
270+ const data = {
271+ title : `${ document . getElementById ( el . dataset . shareGuid ) . dataset . head } ${ def . dataset . lang } meaning` ,
272+ text : `${ def . innerText } ` ,
273+ url : `${ el . href } ` ,
274+ } ;
275+
276+ try {
277+ await navigator . share ( data ) ;
278+ } catch ( err ) {
279+ alert ( `error sharing link: ${ err ?. message || err } ` ) ;
280+ }
281+ } ;
282+ } ) ;
283+ } ) ( ) ;
0 commit comments