如何将网页中的文本选择转换为html标签?

时间:2016-10-17 21:46:44

标签: javascript html bookmarklet

我目前的工作流程要求我从网页复制一段文字并提取html标签。我目前正在选择文本,转到wordtohtml.net,粘贴它,并使用html标签和正确的类信息复制输出。见下文:

enter image description here

是否有一个命令行工具,我可以管理我最初复制的文本并为我做转换?

1 个答案:

答案 0 :(得分:2)

我为你制作了一个书签

  1. 转到网站并将书签拖到书签栏
  2. 选择网站上的任何文字
  3. 点击书签
  4. 从textarea
  5. 复制HTML
  6. 将HTML粘贴到任何您想要的地方
  7. Get HTML of selection bookmarklet

    这是bookmarklets Source-Code

    javascript:(function()%7B(function(window%2Cdocument) %7Bvar range%3Bvar calert%3Bcalert %3D function(text)%7Bvar w %3D Math.max(document.documentElement.clientWidth%2C window.innerWidth %7C%7C 0)%3Bvar h %3D Math.max(document.documentElement.clientHeight%2C window.innerHeight %7C%7C 0)%3Bvar offset %3D %7Btop%3A ((h %2F 2) %2B window.scrollY - 240)%2Cleft%3A ((w %2F 2) - window.scrollX - 320)%7D%3Bvar container %3D document.createElement('div')%3Bcontainer.style.display %3D 'block'%3Bcontainer.style.border %3D '2px solid %23000'%3Bcontainer.style.borderRadius %3D '10px'%3Bcontainer.style.position %3D 'absolute'%3Bcontainer.style.zIndex %3D 99999999%3Bcontainer.style.backgroundColor %3D '%23fff'%3Bcontainer.style.width %3D '340px'%3Bcontainer.style.padding %3D '10px'%3Bcontainer.style.top %3D offset.top %2B 'px'%3Bcontainer.style.left %3D offset.left %2B 'px'%3Bvar contents %3D document.createElement('textarea')%3Bcontents.style.display %3D 'block'%3Bcontents.style.width %3D '310px'%3Bcontents.style.height %3D '240px'%3Bcontents.style.border %3D '1px solid %23ccc'%3Bcontents.value %3D text%3Bvar button %3D document.createElement('button')%3Bbutton.innerHTML %3D 'Close'%3Bbutton.style.display %3D 'block'%3Bbutton.onclick %3D function(evt)%7Bevt.preventDefault()%3Bthis.parentNode.parentNode.removeChild(this.parentNode)%3B%7D%3Bcontainer.appendChild(contents)%3Bcontainer.appendChild(button)%3Bdocument.body.appendChild(container)%3B%7D%3Bif (document.selection %26%26 document.selection.createRange) %7Brange %3D document.selection.createRange()%3Breturn range.htmlText%3B%7Delse if (window.getSelection) %7Bvar selection %3D window.getSelection()%3Bif (selection.rangeCount > 0) %7Brange %3D selection.getRangeAt(0)%3Bvar clonedSelection %3D range.cloneContents()%3Bvar div %3D document.createElement('div')%3Bdiv.appendChild(clonedSelection)%3Bcalert(div.innerHTML)%3B%7Delse %7Bcalert('no selection')%3B%7D%7Delse %7Bcalert('no selection')%3B%7D%7D)(window%2Cdocument)%7D)()