AtCoder Print Task Copy

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

当前为 2020-01-11 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name AtCoder Print Task Copy
  3. // @name:en AtCoder Print Task Copy
  4. // @namespace https://github.com/kichi2004/atcoder-print-task-copy/tree/master
  5. // @version 1.0
  6. // @description AtCoder の印刷用問題文に Copy ボタンを追加します。
  7. // @description:en Add copy buttons to tasks_print page.
  8. // @author kichi2004
  9. // @include https://atcoder.jp/contests/*/tasks_print*
  10. // @grant none
  11. // @require http://code.jquery.com/jquery-1.12.4.min.js
  12. // ==/UserScript==
  13.  
  14. 'use strict';
  15.  
  16. (function () {
  17. if (document.queryCommandSupported('copy')) {
  18. $('#task-statement h3+pre').each(function (i) {
  19. const id = 'pre-sample' + i
  20. $(this).attr('id', id)
  21. const h3 = $(this).prev('h3')
  22. 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>')
  23. $(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>')
  24. })
  25. let cnt = 0
  26. $('pre.prettyprint').each(function () {
  27. const pre_id = 'for_copy' + cnt
  28. 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>'
  29. $(this).before(btn_html)
  30. $(this).addClass('source-code')
  31. const for_copy_html = '<pre id="' + pre_id + '" class="source-code-for-copy"></pre>'
  32. $(this).after($(for_copy_html).text($(this).text()))
  33. cnt++
  34. })
  35. }
  36. $('.btn-copy').click(function () {
  37. window.getSelection().removeAllRanges()
  38. try {
  39. const range = document.createRange()
  40. range.selectNode($('#' + $(this).data('target')).get(0))
  41. window.getSelection().addRange(range)
  42. document.execCommand('copy')
  43. } catch (err) {
  44. console.log(err)
  45. }
  46. window.getSelection().removeAllRanges()
  47. })
  48.  
  49.  
  50. })()
  51.  
  52. // randint
  53. function rand() {
  54. const range = [0, 100000]
  55. return Math.floor(Math.random() * (range[1] - range[0])) + range[0]
  56. }