11import browser from 'webextension-polyfill' ;
2+ import QRCode from 'qrcode' ;
23
34
45// on page load
56document . addEventListener ( 'DOMContentLoaded' , ( ) => {
6- let updatedHTML , html = '<tr class="table__body--holder"><td class="table__body--original"><a href="%longLink%" class="table__body--originalURL" target="_blank" rel="noopener">%longLink%</a></td><td class="table__body--shortened"><div class="table__body--shortenBody"><button class="table__body--copyBtn" title="Copy"><img id="" class="selectDisable icon__img" src="assets/copy.svg" alt="copy" /></button><a href="%shortLink%" class="table__body--shortenURL" target="_blank" rel="noopener">%shortLink%</a></div></td><td class="table__body--functionBtns"><div class="table__body--btnHolder"><button class="table__body--qrcode" title="QR Code"><img id="" class="selectDisable icon__img" src="assets/qrcode.svg" alt="QR Code" /></button><button class="table__body--delete" title="Delete"><img id="" class="selectDisable icon__img" src="assets/delete.svg" alt="Delete" /></button></div></td></tr>' ;
7+ let updatedHTML , html ;
8+ html = '<tr class="table__body--holder" id="table__body-%num%"><td class="table__body--original"><a href="%longLink%" class="table__body--originalURL" target="_blank" rel="noopener">%longLink%</a></td><td class="table__body--shortened"><div class="table__body--shortenBody"><a href="%shortLink%" id="shortUrl-%num%" class="table__body--shortenURL" target="_blank" rel="noopener">%shortLink%</a></div></td><td class="table__body--functionBtns"><div class="table__body--btnHolder" id="btns-%num%"><button class="table__body--copy" id="copy-%num%" title="Copy"><img class="selectDisable icon__img" src="assets/copy.svg" alt="copy" /></button><button class="table__body--qrcode" id="qrcode-%num%" title="QR Code"><img class="selectDisable icon__img" src="assets/qrcode.svg" alt="QR Code" /></button></div></td></tr>' ;
79 // get longURL, shortURL
810 browser . storage . local . get ( [ 'URL_array' , 'count' ] ) . then ( result => {
9- // console.log(result.URL_array);
1011 // update DOM
11- for ( let el of result . URL_array ) {
12- // Regular Expression Based Implementation
13- updatedHTML = html . replace ( / % l o n g L i n k % / g, el . longUrl ) ;
14- updatedHTML = updatedHTML . replace ( / % s h o r t L i n k % / g, el . shortUrl ) ;
15- // inject to DOM
16- document . querySelector ( '.table__content--body' ) . insertAdjacentHTML ( 'afterbegin' , updatedHTML ) ;
12+ if ( result . count > 0 ) {
13+ let pass = 0 ;
14+ for ( let el of result . URL_array ) {
15+ // Regular Expression Based Implementation
16+ updatedHTML = html . replace ( / % l o n g L i n k % / g, el . longUrl ) ;
17+ updatedHTML = updatedHTML . replace ( / % n u m % / g, ++ pass ) ;
18+ updatedHTML = updatedHTML . replace ( / % s h o r t L i n k % / g, el . shortUrl ) ;
19+ // inject to DOM
20+ document . getElementById ( 'delegation__element' ) . insertAdjacentHTML ( 'afterbegin' , updatedHTML ) ;
21+ }
1722 }
1823 } ) ;
1924} ) ;
2025
21- // copy button
2226
23- // QR code gen
27+ function buttonAction ( type , id ) {
28+ if ( type === 'copy' ) {
29+ // copy button
30+ // 1. get url
31+ // 2, add to clipboard
32+ // 3. show tooltip
33+ // console.log('requested copying');
34+ }
35+ else if ( type === 'qrcode' ) {
36+ // inject template
37+ let updatedHTML ;
38+ let html = '<div class="table__qrcodePopup--div" id="qrcode__template"><div class="table__qrcode--popup"><div class="table__qrcode--holder"><img id="table__qrcode" src="%qrcodeLink%" alt="QRCode" /></div><div class="table__closebtn--holder"><button class="table__closebtn--inner" id="close__btn-%num%">Close</button></div></div></div>' ;
39+ // 1. get short link
40+ let shortUrl = document . getElementById ( `shortUrl-${ id } ` ) . textContent ;
41+ // 2. generate qrcode
42+ QRCode . toDataURL ( shortUrl )
43+ . then ( qrcodeURL => {
44+ // 3. display popup menu with link
45+ updatedHTML = html . replace ( '%qrcodeLink%' , qrcodeURL ) ;
46+ updatedHTML = updatedHTML . replace ( '%num%' , id ) ;
47+ document . getElementById ( `btns-${ id } ` ) . insertAdjacentHTML ( 'afterend' , updatedHTML ) ;
48+ } )
49+ . catch ( err => {
50+ // fetch qrcode from http://goqr.me
51+ let qrcode__src = 'https://api.qrserver.com/v1/create-qr-code/?size=120x120&data=' ;
52+ updatedHTML = html . replace ( '%qrcodeLink%' , `${ qrcode__src } ${ shortUrl } ` ) ;
53+ document . getElementById ( `btns-${ id } ` ) . insertAdjacentHTML ( 'afterend' , updatedHTML ) ;
54+ } ) ;
55+ }
56+ else if ( type === 'close__btn' ) {
57+ let el = document . getElementById ( 'qrcode__template' ) ;
58+ el . parentNode . removeChild ( el ) ;
59+ }
60+ }
2461
25- // clear all history
62+
63+ // Clear all history
64+ document . getElementById ( 'table__clearAll--btn' ) . addEventListener ( 'click' , ( ) => {
65+ browser . storage . local . get ( [ 'count' ] ) . then ( result => {
66+ let emptyArray = [ ] ;
67+ if ( result . count > 0 ) {
68+ // empty array in storage and set count to 0
69+ let count = 0 ;
70+ browser . storage . local . set ( { URL_array : emptyArray , count : count } ) . then ( ( ) => {
71+ // remove children
72+ let el = document . getElementById ( 'delegation__element' ) ;
73+ el . parentNode . removeChild ( el ) ;
74+ } ) ;
75+ }
76+ } ) ;
77+ } ) ;
78+
79+
80+ // get the delegation id
81+ function getButtonDetails ( e ) {
82+ let eventId , splitId , type , id ;
83+ eventId = e . target . id ;
84+ if ( eventId ) {
85+ splitId = eventId . split ( '-' ) ;
86+ type = splitId [ 0 ] ;
87+ id = parseInt ( splitId [ 1 ] ) ;
88+ buttonAction ( type , id ) ;
89+ }
90+ }
91+
92+
93+ // Button Action (qrcode / copy)
94+ document . getElementById ( 'delegation__element' ) . addEventListener ( 'click' , getButtonDetails ) ;
0 commit comments