bsn-utilities

工具箱

当前为 2024-05-02 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/486039/1369998/bsn-utilities.js

  1. // 随眠
  2. function sleep(time) {
  3. return new Promise(resolve => setTimeout(resolve, time))
  4. }
  5. // 修改输入框的值(模拟键盘输入)
  6. function setInputValue(input, value) {
  7. input.value = value;
  8. input.dispatchEvent(new InputEvent('input'));
  9. }
  10. // 获取粘贴板文字
  11. async function getClipboardText() {
  12. if (navigator.clipboard && navigator.clipboard.readText) {
  13. const text = await navigator.clipboard.readText();
  14. return text;
  15. }
  16. return "";
  17. }
  18. // 在HTML_ELEMENT中查找所有满足条件的元素,参数innerText可以是字符串(判断innerText是否相等)或REG
  19. function findAllIn(ele, selectors, innerText) {
  20. const arr = Array.from(ele.querySelectorAll(selectors));
  21. return innerText === undefined
  22. ? arr
  23. : typeof innerText === "string"
  24. ? arr.filter(x => x.innerText.trim() === innerText.trim())
  25. : arr.filter(x => x.innerText.trim().match(innerText));
  26. }
  27. // 在HTML_ELEMENT中查找第一个满足条件的元素,参数innerText可以是字符串(判断innerText是否相等)或REG
  28. function findIn(ele, selectors, innerText) {
  29. const arr = findAllIn(ele, selectors, innerText);
  30. return arr.length > 0 ? arr[0] : null;
  31. }
  32. // 在document中查找所有满足条件的元素,参数innerText可以是字符串(判断innerText是否相等)或REG
  33. function findAll(selectors, innerText) {
  34. return findAllIn(document, selectors, innerText);
  35. }
  36. // 在document中查找第一个满足条件的元素,参数innerText可以是字符串(判断innerText是否相等)或REG
  37. function find(selectors, innerText) {
  38. return findIn(document, selectors, innerText);
  39. }
  40. // 选择下拉选项,input为下拉选项元素,wait为等待时间,optionClass为选项的类别,matchFunc为匹配函数(满足条件后触发点击操作)
  41. async function selectOption(input, wait, optionClass, matchFunc) {
  42. input.click();
  43. await sleep(wait);
  44. const optionEles = findAll(optionClass);
  45. const option = optionEles.find((x, i) => matchFunc(x.innerText, i));
  46. if (option) {
  47. option.click();
  48. }
  49. }
  50. // 创建naive对话框,增加异步功能,只能在组件的setup函数里调用
  51. function createNaiveDialog() {
  52. const dialog = naive.useDialog();
  53. ["create", "error", "info", "success", "warning"].forEach(x => {
  54. dialog[x + "Async"] = options => {
  55. return new Promise((resolve,reject) => {
  56. dialog[x]({
  57. ...options,
  58. onNegativeClick: () => resolve(false),
  59. onPositiveClick: () => resolve(true)
  60. });
  61. });
  62. }
  63. });
  64. return dialog;
  65. }
  66. // 初始化Vue3,包括naive及自定义BTable组件
  67. function initVue3(Com) {
  68. const style = document.createElement('style');
  69. style.type = 'text/css';
  70. style.innerHTML=`
  71. .app-wrapper .btn-toggle {
  72. position: fixed;
  73. top: 50vh;
  74. right: 0;
  75. padding-left: 12px;
  76. padding-bottom: 4px;
  77. transform: translateX(calc(100% - 32px)) translateY(-50%);
  78. }
  79. .drawer-wrapper .n-form {
  80. margin: 0 8px;
  81. }
  82. .drawer-wrapper .n-form .n-form-item {
  83. margin: 8px 0;
  84. }
  85. .drawer-wrapper .n-form .n-form-item .n-space {
  86. flex: 1;
  87. }
  88. .drawer-wrapper .n-form .n-form-item .n-input-number {
  89. width: 100%;
  90. }
  91. `;
  92. document.getElementsByTagName('head').item(0).appendChild(style);
  93. const el = document.createElement("div");
  94. el.innerHTML = `
  95. <div id="app" class="app-wrapper"></div>`;
  96. document.body.append(el);
  97.  
  98. const BTable = {
  99. template: `
  100. <table cellspacing="0" cellpadding="0">
  101. <tr v-for="(row, rowIndex) in rows">
  102. <td v-for="cell in row" :rowspan="cell.rowspan" :colspan="cell.colspan" :width="cell.width" :class="cell.class">
  103. <slot :cell="cell">{{cell.value}}</slot>
  104. </td>
  105. </tr>
  106. </table>
  107. `,
  108. props: {
  109. rowCount: Number,
  110. columns: Array, // [{ key: "", label: "", width: "100px", unit: "", editable: false }]
  111. cells: Array // [{ row: 0, col: 0, rowspan: 1, colspan: 1, value: "", useColumnLabel: false }]
  112. },
  113. setup(props) {
  114. const data = Vue.reactive({
  115. rows: Vue.computed(() => {
  116. const arr1 = [];
  117. for(let i = 0; i < props.rowCount; i++) {
  118. const arr2 = [];
  119. for (let j = 0; j < props.columns.length; j++) {
  120. const column = props.columns[j];
  121. const cell = props.cells.find(x => x.row === i && x.col === j);
  122. if (cell) {
  123. const colspan = cell.colspan ?? 1;
  124. arr2.push({
  125. ...cell,
  126. rowspan: cell.rowspan ?? 1,
  127. colspan: colspan,
  128. value: cell.useColumnLabel ? column.label : cell.value,
  129. width: colspan > 1 ? undefined : column.width,
  130. column: column
  131. });
  132. }
  133. }
  134. arr1.push(arr2);
  135. }
  136. return arr1;
  137. })
  138. });
  139. return data;
  140. }
  141. }
  142. const app = Vue.createApp({
  143. template: `
  144. <n-dialog-provider>
  145. <n-message-provider>
  146. <n-button class="btn-toggle" type="primary" round @click="showDrawer=true">
  147. <template #icon>⇆</template>
  148. </n-button>
  149. <n-drawer v-model:show="showDrawer" display-directive="show" resizable class="drawer-wrapper">
  150. <com @closeDrawer="showDrawer=false"/>
  151. </n-drawer>
  152. </n-message-provider>
  153. </n-dialog-provider>
  154. `,
  155. setup() {
  156. const data = Vue.reactive({
  157. showDrawer: false
  158. });
  159. return data;
  160. }
  161. });
  162. app.use(naive);
  163. app.component('b-table', BTable);
  164. app.component('com', Com);
  165. app.mount("#app");
  166. }