FPGA-Plus

More functions for fpgaol (https://fpgaol.ustc.edu.cn/)

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

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