FPGA-Plus

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

当前为 2023-05-29 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name FPGA-Plus
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.4.3
  5. // @description 为 [FPGA Online](https://fpgaol.ustc.edu.cn/) 提供的更多功能
  6. // @author PRO
  7. // @license gpl-3.0
  8. // @match *://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. let name = "FPGA-Plus";
  17. let toast = (s, error=false) => {
  18. if (error) {
  19. console.error(`[${name}] ${s}`);
  20. } else {
  21. console.log(`[${name}] ${s}`);
  22. }
  23. alert(`[${name}] ${s}`);
  24. };
  25. if (window.location.hostname == 'fpgaol.ustc.edu.cn') { // Main page
  26. let navbar = document.querySelector(".navbar-nav");
  27. if (!navbar) return;
  28. let last = navbar.querySelector("form");
  29. if (!last) return;
  30. // Auto navigate to acquired board
  31. let acquired = document.querySelector("table.table.table-striped > tbody > tr:nth-child(4) > td > a");
  32. if (acquired.attributes.href.value != "None") {
  33. acquired.click();
  34. }
  35. // Copy parts
  36. let parts = "xc7a100tcsg324-1";
  37. let hint = document.createElement("li");
  38. hint.classList.add("nav-item");
  39. navbar.insertBefore(hint, last);
  40. let link = document.createElement("a");
  41. link.classList.add("nav-link");
  42. link.text = parts;
  43. link.title = "Click to copy";
  44. link.href = "javascript:void(0);";
  45. link.addEventListener("click", (e) => {
  46. navigator.clipboard.writeText(parts);
  47. if (window.invoke_toastify) {
  48. invoke_toastify();
  49. toast = (s, error = false) => {
  50. window.toast(`[${name}] ${s}`, error);
  51. };
  52. }
  53. toast("Copied!");
  54. });
  55. hint.appendChild(link);
  56. return;
  57. } // Dev page
  58. // Visual improvements
  59. let rsp = document.getElementById("responsetext");
  60. let upload = document.getElementById("upload-button");
  61. upload.addEventListener("click", (e)=>{
  62. rsp.textContent = "Waiting...";
  63. });
  64. // Default upload
  65. let default_input = document.getElementById("bitstream");
  66. let customed = document.getElementById("file-select");
  67. default_input.style.fontSize = "1.3em";
  68. default_input.attributes.removeNamedItem("hidden");
  69. customed.parentNode.replaceChild(default_input, customed);
  70. // Led value
  71. // Pre-process
  72. let val = 0;
  73. let panel = document.querySelector(".col-5.colmodule");
  74. panel.insertAdjacentHTML('afterbegin', '<div class="container"><span id="info" style="padding: inherit;">Bin: 0b00000000; Hex: 0x00; Dec (unsigned): 0</span></div>');
  75. // Functions
  76. function checkbox_patch(checkbox) {
  77. // Check out https://github.com/PRO-2684/gadgets/blob/main/checkbox_patch/ if you're interested in this part
  78. const { get, set } = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'checked');
  79. Object.defineProperty(checkbox, 'checked', {
  80. get() {
  81. return get.call(this);
  82. },
  83. set(newVal) {
  84. let ret = set.call(this, newVal);
  85. this.dispatchEvent(new Event("change"));
  86. return ret;
  87. }
  88. });
  89. }
  90. function get_bit(n) {
  91. return (val >> n) & 1;
  92. }
  93. function set_bit(n, b) {
  94. if (b) {
  95. val |= (1 << n);
  96. } else {
  97. val &= ~(1 << n);
  98. }
  99. }
  100. function update() {
  101. let bin_str = '0b' + val.toString(2).padStart(8, '0');
  102. let hex_str = '0x' + val.toString(16).padStart(2, '0');
  103. let dec_str = val.toString();
  104. let res = `Bin: ${bin_str}; Hex: ${hex_str}; Dec (unsigned): ${dec_str}`;
  105. info.textContent = res;
  106. }
  107. // Setup listeners & init
  108. for (let i = 0; i <= 7; i++) {
  109. let led = document.getElementById(`led${i}`);
  110. set_bit(i, led.checked);
  111. checkbox_patch(led);
  112. led.addEventListener("change", (e) => {
  113. set_bit(i, led.checked);
  114. update();
  115. });
  116. }
  117. update();
  118. })();