From 3dfcd887009a9c1283acd95359abeb57fb06c992 Mon Sep 17 00:00:00 2001 From: vritser Date: Wed, 5 Aug 2020 18:53:18 +0800 Subject: [PATCH 1/3] convert HTML tag to org markup --- background.js | 10 +++--- capture.js | 89 +++++++++++++++++++++++++++++++++++++++++++++++++-- options.html | 8 +++++ options.js | 10 ++++-- 4 files changed, 107 insertions(+), 10 deletions(-) diff --git a/background.js b/background.js index 8d6893f..6065d2b 100644 --- a/background.js +++ b/background.js @@ -29,7 +29,8 @@ chrome.runtime.onInstalled.addListener(function (details) { unselectedTemplate: 'L', useNewStyleLinks: true, debug: false, - overlay: true + overlay: true, + convert2org: false }); else if ((details.reason == "update" && details.previousVersion.startsWith("0.1"))) chrome.storage.sync.set( @@ -38,10 +39,11 @@ chrome.runtime.onInstalled.addListener(function (details) { unselectedTemplate: 'L', useNewStyleLinks: false, debug: false, - overlay: true + overlay: true, + convert2org: false }); }); -chrome.browserAction.onClicked.addListener(function (tab) { - chrome.tabs.executeScript({file: "capture.js"}); +chrome.browserAction.onClicked.addListener(function(tab) { + chrome.tabs.executeScript({ file: "capture.js" }); }); diff --git a/capture.js b/capture.js index 5c1ed2e..56abad8 100644 --- a/capture.js +++ b/capture.js @@ -74,7 +74,12 @@ this.protocol = this.unselectedProtocol; } - for(var k in options) this[k] = options[k]; + for (var k in options) this[k] = options[k]; + + if (this.convert2org) { + this.selection_text = escapeIt(conv2org(window.getSelection())); + } + this.capture(); } } @@ -86,8 +91,86 @@ function escapeIt(text) { return replace_all(replace_all(replace_all(encodeURIComponent(text), "[(]", escape("(")), - "[)]", escape(")")), - "[']" ,escape("'")); + "[)]", escape(")")), + "[']", escape("'")); + } + + function conv2org(sel) { + if (!sel) return ""; + var dom = document.createElement('div'); + for (var i = 0; i < sel.rangeCount; i++) { + dom.appendChild(sel.getRangeAt(i).cloneContents()); + } + + dom.querySelectorAll('img').forEach(it => { + var url = new URL(it.src, window.location.href).href; + it.innerText = `[[${url}]]`; + }); + + dom.querySelectorAll('a').forEach(it => { + if (/\[\[/.test(it.innerText)) + return; + var url = new URL(it.href, window.location.href).href; + it.innerText = `[[${url}][${it.innerText}]]`; + }); + + dom.querySelectorAll('blockquote').forEach(it => { + it.innerHTML = srcBlock('#+begin_quote', '#+end_quote', it.innerText, ""); + }); + + ['p', 'h1', 'h2', 'h3', 'h4'].forEach((tag, idx) => { + dom.querySelectorAll(tag).forEach(it => { + if (idx == 0) { + it.querySelectorAll('code, kbd').forEach(code => { + code.innerText = emphasize('~')(code.innerText); + }); + it.querySelectorAll('b, strong').forEach(b => { + b.innerText = emphasize('*')(b.innerText); + }); + it.querySelectorAll('i, em').forEach(i => { + i.innerText = emphasize('/')(i.innerText); + }); + it.querySelectorAll('del, s').forEach(s => { + s.innerText = emphasize('+')(s.innerText); + }); + } + it.innerHTML = idx > 0 ? `${repeatStar(idx)} ${it.innerText}` : it.innerText; + }); + }); + + dom.querySelectorAll('ul').forEach(ul => { + ul.querySelectorAll('li').forEach(li => { + li.innerText = `- ${li.innerText}`; + }); + }); + + dom.querySelectorAll('pre').forEach(pre => { + var lang = pre.className.match(/lang-(\w+)/) || pre.parentNode.className.match(/highlight-source-(\w+)/); + lang = lang ? lang[1] : ""; + + var code = pre.querySelector('code'); + if (code) { + code.innerHTML = srcBlock('#+begin_src', '#+end_src', code.innerText, lang); + } else { + pre.innerHTML = srcBlock('#+begin_src', '#+end_src', pre.innerText, lang); + } + }); + + return dom.innerText.trim(); + } + + function emphasize(tag) { + return text => `${tag}${text}${tag}`; + } + + function srcBlock(start, end, body, header) { + return `${start} ${header} +${body} +${end}`; + } + + function repeatStar(n) { + return n == 0 ? '' : '*' + repeatStar(n - 1); } function logURI(uri) { diff --git a/options.html b/options.html index 51a222f..8446e67 100644 --- a/options.html +++ b/options.html @@ -42,6 +42,14 @@ + + + Convert HTML to org markup + + + + + Debug (will produce logging messages to console) diff --git a/options.js b/options.js index 1ab186a..9edcd94 100644 --- a/options.js +++ b/options.js @@ -28,13 +28,15 @@ function save_options() { var NewStyleP = document.getElementById('useNewStyle').checked; var debugP = document.getElementById('debug').checked; var overlayP = document.getElementById('overlay').checked; + var orgP = document.getElementById('org').checked; chrome.storage.sync.set({ selectedTemplate: selTemp, unselectedTemplate: unselTemp, useNewStyleLinks: NewStyleP, debug: debugP, - overlay: overlayP + overlay: overlayP, + convert2org: orgP, }, function() { // Update status to let user know options were saved. var status = document.getElementById('status'); @@ -54,15 +56,17 @@ function restore_options() { unselectedTemplate: 'L', useNewStyleLinks: true, debug: false, - overlay: true + overlay: true, + convert2org: false, }, function(options) { document.getElementById('unselTemplate').value = options.unselectedTemplate; document.getElementById('selTemplate').value = options.selectedTemplate; document.getElementById('useNewStyle').checked = options.useNewStyleLinks; document.getElementById('debug').checked = options.debug; document.getElementById('overlay').checked = options.overlay; + document.getElementById('org').checked = options.convert2org; }); } document.addEventListener('DOMContentLoaded', restore_options); document.getElementById('save').addEventListener('click', - save_options); + save_options); From 510834730539cdead8c4711b9780363e87b15864 Mon Sep 17 00:00:00 2001 From: vritser Date: Wed, 5 Aug 2020 23:28:57 +0800 Subject: [PATCH 2/3] code refactoring --- capture.js | 62 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/capture.js b/capture.js index 56abad8..7a3c456 100644 --- a/capture.js +++ b/capture.js @@ -107,37 +107,52 @@ it.innerText = `[[${url}]]`; }); - dom.querySelectorAll('a').forEach(it => { - if (/\[\[/.test(it.innerText)) - return; - var url = new URL(it.href, window.location.href).href; - it.innerText = `[[${url}][${it.innerText}]]`; - }); - dom.querySelectorAll('blockquote').forEach(it => { it.innerHTML = srcBlock('#+begin_quote', '#+end_quote', it.innerText, ""); }); - ['p', 'h1', 'h2', 'h3', 'h4'].forEach((tag, idx) => { + + dom.querySelectorAll('code, kbd').forEach(code => { + code.innerText = emphasize('~')(code.innerText); + }); + dom.querySelectorAll('b, strong').forEach(b => { + b.innerText = emphasize('*')(b.innerText); + }); + dom.querySelectorAll('i, em').forEach(i => { + i.innerText = emphasize('/')(i.innerText); + }); + dom.querySelectorAll('del, s').forEach(s => { + s.innerText = emphasize('+')(s.innerText); + }); + + dom.querySelectorAll('p').forEach(it => { + it.innerHTML = `${it.innerText} +`; + }); + + [, 'h1', 'h2', 'h3', 'h4'].forEach((tag, idx) => { dom.querySelectorAll(tag).forEach(it => { - if (idx == 0) { - it.querySelectorAll('code, kbd').forEach(code => { - code.innerText = emphasize('~')(code.innerText); - }); - it.querySelectorAll('b, strong').forEach(b => { - b.innerText = emphasize('*')(b.innerText); - }); - it.querySelectorAll('i, em').forEach(i => { - i.innerText = emphasize('/')(i.innerText); - }); - it.querySelectorAll('del, s').forEach(s => { - s.innerText = emphasize('+')(s.innerText); - }); + var a = it.querySelector('a'); + console.log("a =====>", a && a.href) + if (a && /#(.+)/.test(a.href)) { + it.innerHTML = `${repeatStar(idx)} [[${a.href}][${it.innerText}]]`; + } else { + it.innerHTML = `${repeatStar(idx)} ${it.innerText}`; } - it.innerHTML = idx > 0 ? `${repeatStar(idx)} ${it.innerText}` : it.innerText; }); }); + dom.querySelectorAll('a').forEach(it => { + if (/\[\[/.test(it.innerText)) + return; + var url = new URL(it.href, window.location.href).href; + if (/#(.+)/.test(it.href)) { + it.innerText = `[[${url}][${it.innerText}]]`; + return; + } + it.innerText = `[[${url}][${it.alt || it.innerText || ''}]]`; + }); + dom.querySelectorAll('ul').forEach(ul => { ul.querySelectorAll('li').forEach(li => { li.innerText = `- ${li.innerText}`; @@ -166,7 +181,8 @@ function srcBlock(start, end, body, header) { return `${start} ${header} ${body} -${end}`; +${end} +`; } function repeatStar(n) { From 495ef7493a3c0016fc284e800dbcbfbbb5528ab2 Mon Sep 17 00:00:00 2001 From: vritser Date: Thu, 6 Aug 2020 00:15:43 +0800 Subject: [PATCH 3/3] fix bug --- capture.js | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/capture.js b/capture.js index 7a3c456..a2f94ae 100644 --- a/capture.js +++ b/capture.js @@ -111,10 +111,18 @@ it.innerHTML = srcBlock('#+begin_quote', '#+end_quote', it.innerText, ""); }); + dom.querySelectorAll('pre').forEach(pre => { + var lang = pre.className.match(/lang-(\w+)/) || pre.parentNode.className.match(/highlight-source-(\w+)/); + lang = lang ? lang[1] : ""; - dom.querySelectorAll('code, kbd').forEach(code => { - code.innerText = emphasize('~')(code.innerText); + var code = pre.querySelector('code'); + if (code) { + code.innerHTML = srcBlock('#+begin_src', '#+end_src', code.innerText, lang); + } else { + pre.innerHTML = srcBlock('#+begin_src', '#+end_src', pre.innerText, lang); + } }); + dom.querySelectorAll('b, strong').forEach(b => { b.innerText = emphasize('*')(b.innerText); }); @@ -125,15 +133,18 @@ s.innerText = emphasize('+')(s.innerText); }); + dom.querySelectorAll('code, kbd').forEach(code => { + if (code.parentNode.nodeName != "PRE") + code.innerText = emphasize('~')(code.innerText); + }); + dom.querySelectorAll('p').forEach(it => { - it.innerHTML = `${it.innerText} -`; + it.innerHTML = `${it.innerText}`; }); [, 'h1', 'h2', 'h3', 'h4'].forEach((tag, idx) => { dom.querySelectorAll(tag).forEach(it => { var a = it.querySelector('a'); - console.log("a =====>", a && a.href) if (a && /#(.+)/.test(a.href)) { it.innerHTML = `${repeatStar(idx)} [[${a.href}][${it.innerText}]]`; } else { @@ -159,18 +170,6 @@ }); }); - dom.querySelectorAll('pre').forEach(pre => { - var lang = pre.className.match(/lang-(\w+)/) || pre.parentNode.className.match(/highlight-source-(\w+)/); - lang = lang ? lang[1] : ""; - - var code = pre.querySelector('code'); - if (code) { - code.innerHTML = srcBlock('#+begin_src', '#+end_src', code.innerText, lang); - } else { - pre.innerHTML = srcBlock('#+begin_src', '#+end_src', pre.innerText, lang); - } - }); - return dom.innerText.trim(); } @@ -181,8 +180,7 @@ function srcBlock(start, end, body, header) { return `${start} ${header} ${body} -${end} -`; +${end}`; } function repeatStar(n) {