bsn-utilities

工具箱

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

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/486039/1370021/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为等待时间,optionParentSelectors为选项父元素的选择器,optionSelectors为选项的选择器,matchFunc为匹配函数(满足条件后触发点击操作)
  41. async function selectOptionIn(input, wait, optionParentSelectors, optionSelectors, matchFunc) {
  42. input.click();
  43. await sleep(wait);
  44. const optionParent = optionParentSelectors ? Array.from(findAll(optionParentSelectors)).find(x => x.style.display !== 'none') : document;
  45. const optionEles = findAllIn(optionParent, optionSelectors);
  46. const option = optionEles.find((x, i) => matchFunc(x.innerText, i));
  47. if (option) {
  48. option.click();
  49. }
  50. }
  51. // 选择下拉选项,input为下拉选项元素,wait为等待时间,optionSelectors为选项的选择器,matchFunc为匹配函数(满足条件后触发点击操作)
  52. async function selectOption(input, wait, optionSelectors, matchFunc) {
  53. await selectOptionIn(input, wait, null, optionSelectors, matchFunc);
  54. }
  55. // 创建naive对话框,增加异步功能,只能在组件的setup函数里调用
  56. function createNaiveDialog() {
  57. const dialog = naive.useDialog();
  58. ["create", "error", "info", "success", "warning"].forEach(x => {
  59. dialog[x + "Async"] = options => {
  60. return new Promise((resolve,reject) => {
  61. dialog[x]({
  62. ...options,
  63. onNegativeClick: () => resolve(false),
  64. onPositiveClick: () => resolve(true)
  65. });
  66. });
  67. }
  68. });
  69. return dialog;
  70. }
  71. // 初始化Vue3,包括naive及自定义BTable组件
  72. function initVue3(Com) {
  73. const style = document.createElement('style');
  74. style.type = 'text/css';
  75. style.innerHTML=`
  76. .app-wrapper .btn-toggle {
  77. position: fixed;
  78. top: 50vh;
  79. right: 0;
  80. padding-left: 12px;
  81. padding-bottom: 4px;
  82. transform: translateX(calc(100% - 32px)) translateY(-50%);
  83. }
  84. .drawer-wrapper .n-form {
  85. margin: 0 8px;
  86. }
  87. .drawer-wrapper .n-form .n-form-item {
  88. margin: 8px 0;
  89. }
  90. .drawer-wrapper .n-form .n-form-item .n-space {
  91. flex: 1;
  92. }
  93. .drawer-wrapper .n-form .n-form-item .n-input-number {
  94. width: 100%;
  95. }
  96. `;
  97. document.getElementsByTagName('head').item(0).appendChild(style);
  98. const el = document.createElement("div");
  99. el.innerHTML = `
  100. <div id="app" class="app-wrapper"></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. template: `
  149. <n-dialog-provider>
  150. <n-message-provider>
  151. <n-button class="btn-toggle" type="primary" round @click="showDrawer=true">
  152. <template #icon>⇆</template>
  153. </n-button>
  154. <n-drawer v-model:show="showDrawer" display-directive="show" resizable class="drawer-wrapper">
  155. <com @closeDrawer="showDrawer=false"/>
  156. </n-drawer>
  157. </n-message-provider>
  158. </n-dialog-provider>
  159. `,
  160. setup() {
  161. const data = Vue.reactive({
  162. showDrawer: false
  163. });
  164. return data;
  165. }
  166. });
  167. app.use(naive);
  168. app.component('b-table', BTable);
  169. app.component('com', Com);
  170. app.mount("#app");
  171. }