Edit Mi

修改店小秘数值

  1. // ==UserScript==
  2. // @name Edit Mi
  3. // @namespace http://tampermonkey.net/
  4. // @license MIT
  5. // @version 2024-11-27
  6. // @description 修改店小秘数值
  7. // @author lyw
  8. // @match https://www.dianxiaomi.com/localTemuProduct/*
  9. // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
  10. // @run-at document-end
  11. // @grant GM_addStyle
  12. // @require https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.14/vue.min.js
  13. // @require https://cdnjs.cloudflare.com/ajax/libs/element-ui/2.15.6/index.min.js
  14. // @resource element-ui-css https://cdnjs.cloudflare.com/ajax/libs/element-ui/2.15.6/theme-chalk/index.min.css
  15. // ==/UserScript==
  16.  
  17. (function () {
  18. 'use strict';
  19.  
  20. // Inject Element UI CSS
  21. GM_addStyle('@import url("https://cdnjs.cloudflare.com/ajax/libs/element-ui/2.15.6/theme-chalk/index.min.css");');
  22.  
  23. // Create a Vue instance for Element UI
  24. const app = document.createElement('div');
  25. document.body.appendChild(app);
  26.  
  27. // Define Vue component with Element UI dialog
  28. new Vue({
  29. el: app,
  30. data: {
  31. dialogVisible: false,
  32. inputValue: ''
  33. },
  34. template: `
  35. <div style="position: absolute; bottom: 16px; right: 689px; z-index: 9999;">
  36. <el-dialog title="输入内容" :visible.sync="dialogVisible" @close="handleClose">
  37. <el-input v-model="inputValue" placeholder="请输入内容" ref="inputField"></el-input>
  38. <span slot="footer" class="dialog-footer">
  39. <el-button type="primary" @click="confirm">确认</el-button>
  40. <el-button @click="dialogVisible = false">取消</el-button>
  41. </span>
  42. </el-dialog>
  43. </div>
  44. `,
  45. methods: {
  46. handleClose() {
  47. this.inputValue = ''; // 清空输入框
  48. },
  49. confirm() {
  50. let processedValue = this.inputValue.replace(/^-+|-+$/g, '').replace(/\s+/g, '');
  51. const inputElements = document.querySelectorAll('.skuInfoList .attrTd .form-component');
  52.  
  53. inputElements.forEach(element => {
  54. const currentValue = element.value;
  55.  
  56. // 如果内容中不包含至少一个 -,则不做修改
  57. if ((currentValue.split('-').length - 1) < 1) {
  58. return; // 不修改该元素
  59. }
  60.  
  61. // 拆分当前值并找到第一个和最后一个 -,然后将它们与新内容组合
  62. const parts = currentValue.split('-');
  63. const newValue = `${parts[0]}-${parts[1]}-${parts[2]}-${processedValue}`;
  64.  
  65. // 使用 execCommand 插入新值 (模拟用户输入)
  66. element.focus();
  67. document.execCommand('selectAll', false, null);
  68. document.execCommand('insertText', false, newValue);
  69.  
  70. // 强制同步 value 和源码
  71. element.setAttribute('value', newValue); // 修改 DOM 中的 value
  72. element.value = newValue; // 修改显示的 value
  73.  
  74. // 手动触发 input 和 change 事件,确保其他脚本能够感知到变化
  75. setTimeout(() => {
  76. element.dispatchEvent(new Event('input', { bubbles: true }));
  77. element.dispatchEvent(new Event('change', { bubbles: true }));
  78. }, 0);
  79. console.log(element.value)
  80. });
  81.  
  82. // 移除焦点,避免焦点仍在最后一个输入框
  83. document.activeElement.blur();
  84.  
  85. this.dialogVisible = false; // 关闭对话框
  86. },
  87. },
  88. mounted() {
  89. // 监听 F2 键触发事件
  90. window.addEventListener('keydown', (event) => {
  91. if (event.key === 'F2') {
  92. this.dialogVisible = true; // 按下 F2 键时显示对话框
  93. }
  94.  
  95. // 监听 Enter 键触发确认操作
  96. if (this.dialogVisible && (event.key === 'Enter' || event.key === 'NumpadEnter')) {
  97. this.confirm(); // 按下 Enter 键时执行 confirm 操作
  98. }
  99. // 监听 Del 键触发删除操作
  100. if (event.key === 'Delete') {
  101. const inputElements = document.querySelectorAll('#skuImgBox .content .m-left0.validformOut');
  102. inputElements.forEach((item)=>{
  103. const test = item.querySelector('.pull-right.m-right6.variantImgDelBtn')
  104. test.click()
  105. })
  106. }
  107. });
  108.  
  109. // 聚焦输入框
  110. this.$watch('dialogVisible', (newVal) => {
  111. if (newVal) {
  112. this.$nextTick(() => {
  113. // 确保 Vue 更新 DOM 后,聚焦输入框
  114. this.$refs.inputField.$el.querySelector('input').focus();
  115. });
  116. }
  117. });
  118. }
  119. });
  120. })();