USACO Enhancements

Adds some enhancements to USACO problems.

  1. // ==UserScript==
  2. // @name USACO Enhancements
  3. // @namespace https://connorcode.com
  4. // @version 0.3
  5. // @description Adds some enhancements to USACO problems.
  6. // @author Connor Slade
  7. // @match http://www.usaco.org/index.php?page=viewproblem*
  8. // @grant GM.getValue
  9. // @grant GM.setValue
  10. // @grant GM.setClipboard
  11. // @license GPLv3
  12. // ==/UserScript==
  13.  
  14. (async () => {
  15. // == Problem year and date in page title ==
  16. let problemDate = document.querySelector('.panel h2').innerText;
  17. let problemName = document.querySelector('.panel h2:nth-child(2)').innerText.split('. ')[1];
  18.  
  19. let date = problemDate.match(/USACO (\d{4}) (.*) Contest/);
  20. document.title = `USACO - ${problemName} (${date[2].substring(0, 3)} ${date[1]})`;
  21.  
  22. // == Persistent language selector ==
  23. let selector = document.querySelector("select[name='language']");
  24.  
  25. let lastLang = await GM.getValue('lastLang');
  26. if (lastLang != undefined) selector.value = lastLang;
  27.  
  28. selector.addEventListener('change', async () => {
  29. console.log(selector.value);
  30. await GM.setValue("lastLang", selector.value);
  31. });
  32.  
  33. // == Copy codeblocks to clipboard ==
  34. document.querySelectorAll('pre').forEach(block => {
  35. block.style.position = 'relative';
  36.  
  37. let copy = document.createElement("button");
  38. copy.style.position = 'absolute';
  39. copy.style.top = 0;
  40. copy.style.right = 0;
  41. copy.innerText = 'Copy';
  42. block.appendChild(copy);
  43. copy.addEventListener('click', () => {
  44. let text = block.innerText;
  45. GM.setClipboard(text.substring(0, text.length - 4));
  46. });
  47. });
  48. })();