AtCoder Copy Contest ID

Add a button to copy the contest ID to the clipboard in AtCoder contest pages

Από την 12/04/2023. Δείτε την τελευταία έκδοση.

  1. // ==UserScript==
  2. // @name AtCoder Copy Contest ID
  3. // @name:ja AtCoder Copy Contest ID
  4. // @namespace https://github.com/xe-o
  5. // @version 0.1
  6. // @description Add a button to copy the contest ID to the clipboard in AtCoder contest pages
  7. // @description:ja AtCoderコンテストページのナビゲーションバーへ、コンテストIDをコピーするためのボタンを追加します
  8. // @author XERO
  9. // @license MIT
  10. // @match https://atcoder.jp/*
  11. // @grant GM_setClipboard
  12. // @run-at document-idle
  13. // ==/UserScript==
  14.  
  15. const COPY_BUTTON_LABEL_INIT = "Copy Contest ID";
  16. const COPY_BUTTON_HTML = `
  17. <li>
  18. <a id="contest-id-copy-button" style="cursor: pointer; font-size: 12px">
  19. <span class="glyphicon glyphicon-copy" style="margin-right: 2px" aria-hidden="true"></span>
  20. <span id="copy-button-text">${COPY_BUTTON_LABEL_INIT}</span>
  21. </a>
  22. </li>`;
  23.  
  24. const $ = (selector) => document.querySelector(selector);
  25. const getContestID = () => window.location.pathname.split("/")[2];
  26.  
  27. const navbarElement = $(".navbar-nav");
  28. if (!navbarElement) throw new Error("Navbar element not found.");
  29. navbarElement.insertAdjacentHTML("beforeend", COPY_BUTTON_HTML);
  30.  
  31. const copyButton = $("#contest-id-copy-button");
  32. const copyButtonIcon = $(".glyphicon", copyButton);
  33. const copyButtonLabel = $("#copy-button-text");
  34.  
  35. async function copyToClipboard() {
  36. try {
  37. await GM_setClipboard(getContestID(), {
  38. type: "text",
  39. mimetype: "text/plain",
  40. });
  41. copyButtonIcon.classList.replace("glyphicon-copy", "glyphicon-ok");
  42. } catch (error) {
  43. console.error(`Failed to copy contest ID: ${error}`);
  44. copyButtonIcon.classList.replace("glyphicon-copy", "glyphicon-remove");
  45. } finally {
  46. const copyResultText = copyButtonIcon.classList.contains("glyphicon-ok")
  47. ? "Copied!"
  48. : "Failed to copy.";
  49. copyButtonLabel.textContent = copyResultText;
  50. setTimeout(() => {
  51. copyButtonLabel.textContent = COPY_BUTTON_LABEL_INIT;
  52. copyButtonIcon.classList.replace(
  53. "glyphicon-ok",
  54. "glyphicon-copy",
  55. "glyphicon-remove"
  56. );
  57. }, 1800);
  58. }
  59. }
  60.  
  61. copyButton.addEventListener("click", copyToClipboard);