diff options
author | Feuerfuchs <git@feuerfuchs.dev> | 2019-06-26 20:16:19 +0200 |
---|---|---|
committer | Feuerfuchs <git@feuerfuchs.dev> | 2019-06-26 20:16:19 +0200 |
commit | 3bcc0b99e2d221243c3bcc2a5cc2a7f46c6a6d59 (patch) | |
tree | 94dec3e5d1203992e23e8c59c38acc63502f5aa7 /js | |
parent | Better ellipsis for location overflow (diff) | |
download | gopherproxy-3bcc0b99e2d221243c3bcc2a5cc2a7f46c6a6d59.tar.gz gopherproxy-3bcc0b99e2d221243c3bcc2a5cc2a7f46c6a6d59.tar.bz2 gopherproxy-3bcc0b99e2d221243c3bcc2a5cc2a7f46c6a6d59.zip |
New feature: Clickable links in plain text (opt)
Diffstat (limited to 'js')
-rw-r--r-- | js/main.ts | 77 |
1 files changed, 74 insertions, 3 deletions
@@ -20,6 +20,10 @@ const settings = new KeyValueStore({ | |||
20 | value: ensureSetting('image-previews', '1') === '1', | 20 | value: ensureSetting('image-previews', '1') === '1', |
21 | valueRange: [false, true] | 21 | valueRange: [false, true] |
22 | }, | 22 | }, |
23 | clickablePlainLinks: { | ||
24 | value: ensureSetting('clickable-plain-links', '1') === '1', | ||
25 | valueRange: [false, true] | ||
26 | }, | ||
23 | }); | 27 | }); |
24 | 28 | ||
25 | // | 29 | // |
@@ -46,8 +50,13 @@ const settings = new KeyValueStore({ | |||
46 | locationPrefixEl.addEventListener('click', e => { | 50 | locationPrefixEl.addEventListener('click', e => { |
47 | e.preventDefault(); | 51 | e.preventDefault(); |
48 | 52 | ||
49 | const resp = prompt('Please enter new location: ', ''); | 53 | let resp = prompt('Please enter new location: ', ''); |
50 | if ((resp !== null) && (resp !== "")) { | 54 | if ((resp !== null) && (resp.trim() !== "")) { |
55 | resp = resp.trim(); | ||
56 | if (resp.indexOf('gopher://') === 0) { | ||
57 | resp = resp.substring(9); | ||
58 | } | ||
59 | |||
51 | window.location.href = window.location.origin + '/' + resp; | 60 | window.location.href = window.location.origin + '/' + resp; |
52 | } | 61 | } |
53 | 62 | ||
@@ -93,7 +102,7 @@ const settings = new KeyValueStore({ | |||
93 | contentEl.classList.remove("content--wrap"); | 102 | contentEl.classList.remove("content--wrap"); |
94 | } | 103 | } |
95 | 104 | ||
96 | localStorage.setItem('image-previews', value ? '1' : '0'); | 105 | localStorage.setItem('word-wrap', value ? '1' : '0'); |
97 | settingWordWrapValueEl.textContent = value ? '[yes]' : '[no]'; | 106 | settingWordWrapValueEl.textContent = value ? '[yes]' : '[no]'; |
98 | } | 107 | } |
99 | 108 | ||
@@ -105,6 +114,30 @@ const settings = new KeyValueStore({ | |||
105 | 114 | ||
106 | settingWordWrapCallback(settings.getValue('wordWrap')); | 115 | settingWordWrapCallback(settings.getValue('wordWrap')); |
107 | settings.addCallback('wordWrap', settingWordWrapCallback); | 116 | settings.addCallback('wordWrap', settingWordWrapCallback); |
117 | |||
118 | // | ||
119 | |||
120 | const clickablePlainLinksEl = document.getElementsByClassName('setting--clickable-plain-links')[0]; | ||
121 | const clickablePlainLinksValueEl = clickablePlainLinksEl.getElementsByClassName('setting__value')[0]; | ||
122 | const clickablePlainLinksCallback = (value: boolean) => { | ||
123 | if (value) { | ||
124 | generateMarkupForPlainLinks(); | ||
125 | } else { | ||
126 | removeMarkupForPlainLinks(); | ||
127 | } | ||
128 | |||
129 | localStorage.setItem('clickable-plain-links', value ? '1' : '0'); | ||
130 | clickablePlainLinksValueEl.textContent = value ? '[yes]' : '[no]'; | ||
131 | } | ||
132 | |||
133 | clickablePlainLinksValueEl.addEventListener('click', e => { | ||
134 | e.preventDefault(); | ||
135 | settings.cycleValue('clickablePlainLinks'); | ||
136 | return false; | ||
137 | }); | ||
138 | |||
139 | clickablePlainLinksCallback(settings.getValue('clickablePlainLinks')); | ||
140 | settings.addCallback('clickablePlainLinks', clickablePlainLinksCallback); | ||
108 | })(); | 141 | })(); |
109 | 142 | ||
110 | (() => { | 143 | (() => { |
@@ -211,3 +244,41 @@ function removeImageThumbnails() { | |||
211 | } | 244 | } |
212 | } | 245 | } |
213 | } | 246 | } |
247 | |||
248 | function generateMarkupForPlainLinks() { | ||
249 | if (!document.body.classList.contains('is-plain')) { | ||
250 | return; | ||
251 | } | ||
252 | |||
253 | const contentEl = document.getElementsByClassName('content')[0]; | ||
254 | const urlRegex = /\b([a-z]*:\/\/[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b[-a-zA-Z0-9@:%_\+.~#?&//=]*)\b/g; | ||
255 | const mailRegex = /\bmailto:[-a-zA-Z0-9@:%._\+~#=]+@(?:[-a-zA-Z0-9@:%._\+~#=]+\.)+[a-z]{2,4}\b/g; | ||
256 | |||
257 | contentEl.innerHTML = contentEl.innerHTML.replace(urlRegex, match => { | ||
258 | let href: string = match; | ||
259 | if (href.indexOf('gopher://') === 0) { | ||
260 | href = href.replace(/^gopher:\/\/(.*)$/, location.origin + '/$1'); | ||
261 | } | ||
262 | return `<a href="${href}">${match}</a>`; | ||
263 | }); | ||
264 | |||
265 | contentEl.innerHTML = contentEl.innerHTML.replace(mailRegex, match => { | ||
266 | return `<a href="${match}">${match}</a>`; | ||
267 | }); | ||
268 | } | ||
269 | |||
270 | function removeMarkupForPlainLinks() { | ||
271 | if (!document.body.classList.contains('is-plain')) { | ||
272 | return; | ||
273 | } | ||
274 | |||
275 | const contentEl = document.getElementsByClassName('content')[0]; | ||
276 | const anchorEls = contentEl.getElementsByTagName('a'); | ||
277 | let i = anchorEls.length; | ||
278 | while (i--) { | ||
279 | const anchorEl = anchorEls[i]; | ||
280 | const textNodeEl = document.createTextNode(anchorEl.textContent!); | ||
281 | |||
282 | contentEl.replaceChild(textNodeEl, anchorEl); | ||
283 | } | ||
284 | } | ||