装飾そのままコピー
Webからコピペしたものを、Scrapbox表記に変換する
動いてない気がする/tkgshn/tkgshn.icon2021/3/13
動いてる2021/4/28
code:script.js
if (sessionStorage.getItem('scrasobox-paste') !== 'true') {
document.addEventListener('paste', event => {
const type = 'text/html'
const d = event.clipboardData
if (d.types && d.types.indexOf('Files') == -1 && -1 < d.types.indexOf(type)) {
event.preventDefault()
const dom = new DOMParser().parseFromString(d.getData(type), type)
const ng = text => text.trim().replace(/[\\\n]/g, ' ') const q = query => dom.querySelectorAll(query)
const f = (text, deco) =>
[].concat(text.split(/\n/)
.filter(l => l.trim()).map(l => [${deco} ${l.trim()}] )).join('')
q('pre').forEach(d => {
d.innerText = 'code:code.*\n' + d.innerText.split('\n').map(l => ${l}).join('\n') })
q('h3').forEach(d => { d.innerText = f(d.innerText, '**') })
q('h2').forEach(d => { d.innerText = f(d.innerText, '***') })
q('h1').forEach(d => { d.innerText = f(d.innerText, '****') })
q('em, i').forEach(d => { d.innerText = f(d.innerText, '/') })
q('strong, b').forEach(d => { d.innerText = f(d.innerText, '*') })
d.closest('a').outerHTML = [${d.src.trim()}#.png ${d.closest('a').href}] })
q('ahref').forEach(d => { d.outerHTML = [${d.href} ${ng(d.text).trim()}] }) q('imgsrc').forEach(d => { d.outerHTML = [${d.src.trim()}#.png] }) d.outerHTML = [https://www.youtube.com/watch?v=${d.src.split('/embed/')[1].split('?')[0]}] })
q('code').forEach(d => { d.innerText = '' + d.innerText + '' })
let depth = -1
const li = node => {
depth++
node.querySelectorAll('li').forEach(n => li(n))
return node.innerHTML = '@sp@'.repeat(depth--) + node.innerHTML
}
li(dom.body)
const div = document.createElement('div')
div.innerHTML = dom.body.innerHTML
document.body.appendChild(div)
const range = document.createRange()
range.selectNode(div)
//const text = range.toString()
const text = div.innerText
document.execCommand('insertText', null, text.replace(/(\s*\n){3,}/g, '\n\n').replace(/@sp@/gi, ' '))
document.body.removeChild(div)
}
})
sessionStorage.setItem('scrasobox-paste', true)
}
window.addEventListener('unload', () => { sessionStorage.setItem('scrasobox-paste', false) })