AtCoder Print Task Copy

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

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==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]
}