FPGA-Plus

为 [FPGA Online](https://fpgaol.ustc.edu.cn/) 提供的更多功能

目前为 2023-05-07 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name FPGA-Plus
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description 为 [FPGA Online](https://fpgaol.ustc.edu.cn/) 提供的更多功能
  6. // @author PRO
  7. // @license gpl-3.0
  8. // @match https://fpgaol.ustc.edu.cn/*
  9. // @match http://202.38.79.134:*/*
  10. // @grant none
  11. // @icon https://fpgaol.ustc.edu.cn/favicon.ico
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16. if (window.location.hostname == 'fpgaol.ustc.edu.cn') {
  17. let navbar = document.querySelector(".navbar-nav");
  18. if (!navbar) return;
  19. let last = navbar.querySelector("form");
  20. if (!last) return;
  21. let hint = document.createElement("li");
  22. hint.classList.add("nav-item");
  23. hint.innerHTML = '<a class="nav-link" title="Click to copy" href="javascript:navigator.clipboard.writeText(\'xc7a100tcsg324-1\');">xc7a100tcsg324-1</a>';
  24. navbar.insertBefore(hint, last);
  25. return;
  26. }
  27. // Pre-process
  28. let val = 0;
  29. let panel = document.querySelector(".col-5.colmodule");
  30. panel.insertAdjacentHTML('afterbegin', '<div class="container"><span id="info" style="padding: inherit;">Bin: 0b00000000; Hex: 0x00; Dec (unsigned): 0</span></div>');
  31. // Functions
  32. function checkbox_patch(checkbox) {
  33. // Check out https://github.com/PRO-2684/gadgets/blob/main/checkbox_patch/ if you're interested in this part
  34. const { get, set } = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'checked');
  35. Object.defineProperty(checkbox, 'checked', {
  36. get() {
  37. return get.call(this);
  38. },
  39. set(newVal) {
  40. let ret = set.call(this, newVal);
  41. this.dispatchEvent(new Event("change"));
  42. return ret;
  43. }
  44. });
  45. }
  46. function get_bit(n) {
  47. return (val >> n) & 1;
  48. }
  49. function set_bit(n, b) {
  50. if (b) {
  51. val |= (1 << n);
  52. } else {
  53. val &= ~(1 << n);
  54. }
  55. }
  56. function update() {
  57. let bin_str = '0b' + val.toString(2).padStart(8, '0');
  58. let hex_str = '0x' + val.toString(16).padStart(2, '0');
  59. let dec_str = val.toString();
  60. let res = `Bin: ${bin_str}; Hex: ${hex_str}; Dec (unsigned): ${dec_str}`;
  61. info.textContent = res;
  62. }
  63. // Setup listeners & init
  64. for (let i = 0; i <= 7; i++) {
  65. let led = document.getElementById(`led${i}`);
  66. set_bit(i, led.checked);
  67. checkbox_patch(led);
  68. led.addEventListener("change", (e) => {
  69. set_bit(i, led.checked);
  70. update();
  71. });
  72. }
  73. update();
  74. })();