立创商城bom匹配结果排序

Supported by GPT

  1. // ==UserScript==
  2. // @name 立创商城bom匹配结果排序
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Supported by GPT
  6. // @author tumuyan
  7. // @match https://bom.szlcsc.com/member/eda/search.html*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // 创建悬浮窗
  17. const floatingDiv = document.createElement('div');
  18. floatingDiv.style.position = 'fixed';
  19. floatingDiv.style.top = '10px';
  20. floatingDiv.style.left = '10px'; // 改为左上角
  21. floatingDiv.style.backgroundColor = 'white';
  22. floatingDiv.style.border = '1px solid #ccc';
  23. floatingDiv.style.padding = '10px';
  24. floatingDiv.style.zIndex = '16777271';
  25. floatingDiv.style.boxShadow = '0 2px 10px rgba(0,0,0,0.2)';
  26.  
  27. // 添加提示文本
  28. const label = document.createElement('label');
  29. label.innerText = '排序方式: ';
  30. floatingDiv.appendChild(label);
  31.  
  32. // 创建下拉菜单
  33. const select = document.createElement('select');
  34. const options = ['品牌', '名称', '封装', '广东仓', '江苏仓', '交期','MOQ'];
  35.  
  36. // 默认第一个选项为空
  37. const defaultOption = document.createElement('option');
  38. defaultOption.value = '';
  39. defaultOption.innerText = '请选择';
  40. select.appendChild(defaultOption);
  41.  
  42. options.forEach(option => {
  43. const opt = document.createElement('option');
  44. opt.value = option;
  45. opt.innerText = option;
  46. select.appendChild(opt);
  47. });
  48.  
  49. // 处理选择变化
  50. select.addEventListener('change', function() {
  51. const orderby = select.value ? select.value + ':' : '';
  52. highlightSelectedOption(select);
  53. if (orderby) {
  54. sortTable(orderby);
  55. }
  56. });
  57.  
  58. floatingDiv.appendChild(select);
  59. document.body.appendChild(floatingDiv);
  60.  
  61. // 高亮选中的选项
  62. function highlightSelectedOption(selectElement) {
  63. Array.from(selectElement.options).forEach(option => {
  64. option.style.backgroundColor = option.selected ? '#d3d3d3' : '';
  65. });
  66. }
  67.  
  68. // 排序表格
  69. function sortTable(orderby) {
  70. const rows = Array.from(document.querySelectorAll('tr.el-table__row'));
  71. rows.sort((a, b) => {
  72. const textA = getTextAfterLabel(a, orderby.replace(':', ''));
  73. const textB = getTextAfterLabel(b, orderby.replace(':', ''));
  74. const numA = parseFloat(textA);
  75. const numB = parseFloat(textB);
  76.  
  77. // 如果都可以解析为正数,优先按数值排序
  78. if (!isNaN(numA) && !isNaN(numB)) {
  79. return numA - numB;
  80. } else if (!isNaN(numA)) {
  81. return -1; // numA 是数值,排在前面
  82. } else if (!isNaN(numB)) {
  83. return 1; // numB 是数值,排在前面
  84. } else {
  85. return textA.localeCompare(textB); // 否则按文本排序
  86. }
  87. });
  88.  
  89. const tableBody = document.querySelector('tbody');
  90. if (tableBody) {
  91. tableBody.innerHTML = '';
  92. rows.forEach(row => {
  93. tableBody.appendChild(row);
  94. });
  95. }
  96. }
  97.  
  98. // 获取指定元素之后的文本
  99. function getTextAfterLabel(row, label) {
  100. const cells = row.querySelectorAll('td');
  101. for (let cell of cells) {
  102. if (cell.textContent.includes(label + ':')) {
  103. const text = cell.textContent.split(label + ':')[1];
  104. return text ? text.trim() : '';
  105. }
  106. }
  107. return '';
  108. }
  109.  
  110. // 页面重载时恢复为第一个选项
  111. window.addEventListener('load', () => {
  112. select.selectedIndex = 0; // 恢复为第一个选项
  113. highlightSelectedOption(select);
  114. });
  115. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址