FPGA-Plus

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

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

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