就不登录-csdn

复制网页内容

As of 2024-05-20. See the latest version.

// ==UserScript==
// @name        就不登录-csdn
// @license MIT
// @namespace   unregistered
// @match       http*://blog.csdn.net/*/article/details/**
// @noframes
// @grant       none
// @version     1.2.2
// @author      ojer
// @description 复制网页内容
// ==/UserScript==

const btnText = '复制'
let editorType = void 0
const copyBtn = 'hljs-button'
const codeElement = document.querySelectorAll('pre code')

const selectById = (e) => {
  if (((e = 'string' == typeof e ? document.querySelector(e) : e), navigator.userAgent.match(/ipad|ipod|iphone/i))) {
    const range = document.createRange()
    const selection = window.getSelection()
    const contentEdit = e.contentEditable
    const readOnly = e.readOnly
    e.contentEditable = true
    e.readOnly = true
    range.selectNodeContents(e)
    selection.removeAllRanges()
    selection.addRange(range)
    e.setSelectionRange(0, 999999)
    e.contentEditable = contentEdit
    e.readOnly = readOnly
  } else {
    e.select()
  }
}

const setStyle = () => {
  var e = document.createElement('style')
  e.setAttribute(type , 'text/css')
  e.innerHTML = ['code, pre {user-select: auto !important;}', '#article_content{height: auto !important;}', '.hide-article-box.hide-article-pos.text-center{display: none !important;}']
    .join('')
    .format(copyBtn)
  document.getElementsByTagName('head')[0].appendChild(e)
}

const execCopy = (e, text) => {
  var t = e.target || e.srcElement
  var n = document.documentElement.scrollTop
  if (t.className.indexOf(copyBtn) > -1) {
    e.preventDefault()
    var o = document.getElementById('hljs-copy-el')
    o || ((o = document.createElement('textarea')), (o.style.position = 'absolute'), (o.style.left = '-9999px'), (o.style.top = n + 'px'), (o.id = 'hljs-copy-el'), document.body.appendChild(o)),
      (o.textContent = text.replace(/[\u00A0]/gi, ' ')),
      selectById('#hljs-copy-el')
    try {
      var r = document.execCommand('copy')
      o.remove(),
        (t.dataset.title = r ? '复制成功' : '复制失败'),
        r &&
          setTimeout(function () {
            t.dataset.title = btnText
          }, 3e3)
    } catch (i) {
      t.dataset.title = '复制失败'
    }
  }
}
const copyCode = (e) => {
  execCopy(e, e.currentTarget.innerText)
}

const copyParentNodeCode = (e) => {
  execCopy(e, e.currentTarget.parentNode.innerText)
}

const addCopyBtnByType = () => {
  try {
    var e = undefined
    if ('ckeditor' == editorType) {
      e = document.querySelectorAll('code.hljs')
      for (var t in e) {
        e.hasOwnProperty(t) && addCopyButton(e[t].parentNode)
      }
    } else {
      e = codeElement
      for (var t in e) e.hasOwnProperty(t) && addCopyButton(e[t])
    }
  } catch (n) {
    console.error('CopyButton error: ', n)
  }
}

const addCopyButton = (e) => {
  if (e === null || 'object' !== typeof e) {
    return
  }
  var t = ''
  var n = '.signin(event)'
  var o = 'hljs'
  var clickFun = '.copyCode(event)'
  'mdeditor' === editorType && (o = 'mdcp'),
    (clickFun = o + clickFun),
    ((clickFun = o + n), (t = 'data-report-click=\'{"spm":"1001.2101.3001.4334"}\'')),
    (e.innerHTML = e.innerHTML + ('<div class="{0} {2}" data-title="{1}" ' + t + '></div>').format(copyBtn, btnText, '')),
    'hljs' === o ? e.querySelector('.hljs-button').setAttribute('onclick', clickFun) : (e.setAttribute('onclick', clickFun), (e.style.position = 'unset'))
}

const initCopy = () => {
  document.querySelectorAll('.hljs-button').forEach((e) => {
    e.remove()
  })

  let i = 0
  const intervalId = setInterval(() => {
    if (this.unsafeWindow.csdn && this.unsafeWindow.csdn.loginBox && this.unsafeWindow.csdn.loginBox.show) {
      this.unsafeWindow.csdn.loginBox.show = () => {
        return 0
      }
      clearInterval(intervalId)
    }

    if (i++ > 100) {
      clearInterval(intervalId)
    }
  }, 2e3)

  const onCopy = (t) => {
    if (window.getSelection().getRangeAt(0).toString().length > 0) {
      t.preventDefault()
      var e = window
        .getSelection()
        .getRangeAt(0)
        .toString()
        .replace(/[\u00A0]/gi, ' ')
      if (t.clipboardData) {
        t.clipboardData.setData('text/plain', e)
      } else {
        if (window.clipboardData) {
          return window.clipboardData.setData('text', e)
        }
        var e = document.createElement('textarea')
        ;(e.style.cssText = 'position: fixed;z-index: -10;top: -50px;left: -50px;'),
          (e.innerHTML = t),
          document.getElementsByTagName('body')[0].appendChild(e),
          e.select(),
          document.execCommand('copy')
      }
    }
  }

  document.querySelector('#content_views').addEventListener('copy', onCopy)
  document.querySelector('#content_views').addEventListener('keydown', onCopy)
  addCopyBtnByType()
}

const init = () => {
  const winw = this.unsafeWindow
  if (document.querySelectorAll('div.htmledit_views').length > 0) {
    editorType = 'ckeditor'
    Object.assign(winw.hljs, {
      initCopyButtonOnLoad: initCopy,
      addCopyButton: addCopyButton,
      copyCode: copyParentNodeCode,
      signin: copyParentNodeCode
    })
  } else {
    if (codeElement.length > 0) {
      editorType = 'mdeditor'
      if (!winw.mdcp) {
        winw.mdcp = {}
      }
      Object.assign(winw.mdcp, {
        copyCode: copyCode,
        signin: copyCode
      })
    }
  }
  initCopy()
  setStyle()
}

const intervalId = setInterval(() => {
  if ('complete' === document.readyState) {
    setTimeout(() => {
      init()
    }, 5e2)
    clearInterval(intervalId)
  }
}, 1e3)