bsn-utilities

工具箱

目前為 2024-01-30 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/486039/1319699/bsn-utilities.js

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