AtCoder Print Task Copy

AtCoder の印刷用問題文に Copy ボタンを追加します。

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name           AtCoder Print Task Copy
// @name:en        AtCoder Print Task Copy
// @namespace      https://github.com/kichi2004/atcoder-print-task-copy/tree/master
// @version        1.0
// @description    AtCoder の印刷用問題文に Copy ボタンを追加します。
// @description:en Add copy buttons to tasks_print page.
// @author         kichi2004
// @include        https://atcoder.jp/contests/*/tasks_print*
// @grant          none
// @require        http://code.jquery.com/jquery-1.12.4.min.js
// ==/UserScript==

'use strict';

(function () {
  if (document.queryCommandSupported('copy')) {
    $('#task-statement h3+pre').each(function (i) {
      const id = 'pre-sample' + i
      $(this).attr('id', id)
      const h3 = $(this).prev('h3')
      h3.append(' <span class="btn btn-default btn-sm btn-copy" tabindex="0" data-toggle="tooltip" data-trigger="manual" title="Copied!" data-target="' + id + '">Copy</span>')
      $(this).before('<div class="div-btn-copy"><span class="btn-copy btn-pre" tabindex="0" data-toggle="tooltip" data-trigger="manual" title="Copied!" data-target="' + id + '">Copy</span></div>')
    })
    let cnt = 0
    $('pre.prettyprint').each(function () {
      const pre_id = 'for_copy' + cnt
      const btn_html = '<div class="div-btn-copy"><span class="btn-copy btn-pre" tabindex="0" data-toggle="tooltip" data-trigger="manual" title="Copied!" data-target="' + pre_id + '">Copy</span></div>'
      $(this).before(btn_html)
      $(this).addClass('source-code')
      const for_copy_html = '<pre id="' + pre_id + '" class="source-code-for-copy"></pre>'
      $(this).after($(for_copy_html).text($(this).text()))
      cnt++
    })
  }
  $('.btn-copy').click(function () {
    window.getSelection().removeAllRanges()
    try {
      const range = document.createRange()
      range.selectNode($('#' + $(this).data('target')).get(0))
      window.getSelection().addRange(range)
      document.execCommand('copy')
    } catch (err) {
      console.log(err)
    }
    window.getSelection().removeAllRanges()
  })


})()

// randint
function rand() {
  const range = [0, 100000]
  return Math.floor(Math.random() * (range[1] - range[0])) + range[0]
}