Last active
June 3, 2020 00:36
-
-
Save Wardrop/fc337d079a6ec36a53e37d0f2d4c7f26 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Gentu Paster | |
// @namespace Violentmonkey Scripts | |
// @match https://gentu.io/patients/*/clinical_history/letters/* | |
// @grant none | |
// @version 1.0 | |
// @author - | |
// @require https://cdn.jsdelivr.net/gh/CoeJoder/waitForKeyElements.js@v1.2/waitForKeyElements.js | |
// @description 26/05/2020, 20:58:38 | |
// ==/UserScript== | |
window.addEventListener('load', function() { | |
waitForKeyElements("[contenteditable]", (element) => { | |
element.addEventListener('paste', (e) => { | |
setTimeout(() => { | |
for(table of element.querySelectorAll('table:not(.layout-table)')) { | |
table.classList.add('layout-table') | |
} | |
}, 100) | |
console.log('pasted into contenteditable') | |
}) | |
}); | |
document.addEventListener('keyup', (e) => { | |
if(e.key.toLowerCase() == 'v' && e.altKey == true && e.ctrlKey == true) { | |
var div = document.createElement('div') | |
div.style.cssText = 'position: fixed; top: 10pt; bottom: 10pt; left: 10pt; right: 10pt; box-shadow: 3px 3px 10px rgba(0,0,0,0.25); border: 1px solid grey; padding: 8pt; z-index: 2000; background: white; border-radius: 4pt;' | |
div.setAttribute('contenteditable', true) | |
document.body.appendChild(div) | |
div.addEventListener('paste', (e) => { | |
var pastedHtml = e.clipboardData.getData('text/html') | |
if(pastedHtml.length > 0) { | |
setTimeout(() => { | |
clean(div) | |
}, 100) | |
} | |
}) | |
div.addEventListener('copy', (e) => { | |
setTimeout(() => { div.remove() }, 100) | |
}) | |
} | |
}) | |
}, false); | |
function clean(parent) { | |
function unwrap(wrapper) { | |
// place childNodes in document fragment | |
var docFrag = document.createDocumentFragment(); | |
while (wrapper.firstChild) { | |
var child = wrapper.removeChild(wrapper.firstChild); | |
docFrag.appendChild(child); | |
} | |
// replace wrapper with document fragment | |
wrapper.parentNode.replaceChild(docFrag, wrapper); | |
} | |
for (node of parent.querySelectorAll('*')) { | |
node.removeAttribute('colspan') | |
} | |
for (node of parent.querySelectorAll('*')) { | |
if (node.style.getPropertyValue('text-decoration-line')) { | |
node.style.removeProperty('text-decoration-line') | |
wrapped = "<u>" + node.innerHTML + "</u>"; | |
node.innerHTML = wrapped; | |
} | |
} | |
for (node of parent.querySelectorAll('*')) { | |
if (node.style.getPropertyValue('font-weight') == 'bold') { | |
node.style.removeProperty('font-weight') | |
wrapped = "<strong>" + node.innerHTML + "</strong>"; | |
node.innerHTML = wrapped; | |
} | |
} | |
for (node of parent.querySelectorAll('a')) { | |
unwrap(node) | |
} | |
// For tables consisting of a single row, which are themselves direct deescendants of another table, append row to parent table. | |
for (table of parent.querySelectorAll('td > table')) { | |
if (table.rows.length == 1) { | |
table.closest('tr').insertAdjacentElement('afterend', table.rows[0]) | |
} | |
} | |
for (n of parent.querySelectorAll('[style]')) { | |
n.removeAttribute('style') | |
} | |
// Remove wrapping table if all child rows only have a single cell | |
for (table of parent.querySelectorAll('table')) { | |
var singleCelled = [...table.rows].every( (x) => { return x.cells.length <= 1 }) | |
if (singleCelled) { | |
for(var row of table.rows) { | |
var cell = row.cells[0] | |
if (cell) { | |
var div = document.createElement('div') | |
while (cell.firstChild) { | |
div.appendChild(cell.firstChild) | |
} | |
table.insertAdjacentElement('beforebegin', div); | |
} | |
} | |
table.remove() | |
} | |
} | |
var walk = document.createTreeWalker(parent,NodeFilter.SHOW_TEXT,null,false); | |
while(node = walk.nextNode()) { | |
node.nodeValue = node.nodeValue.replace(/( {2,})/gi,'').trim() | |
} | |
for (n of parent.querySelectorAll('*')) { | |
if (n.innerText == '') { | |
n.remove() | |
} else { | |
if (n.tagName == 'STRONG') { | |
n.prepend(document.createElement('br')); | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment