const fs = require('fs'); const path = 'e:\\I code\\pages\\index\\index.js'; try { let content = fs.readFileSync(path, 'utf8'); // Define old and new function // We use a simple string replace for the function head and body start const oldFn = ` revealNext: function (e) { const index = e.currentTarget.dataset.index;`; const newFn = ` revealNext: function (e) { const index = (e.detail && e.detail.index !== undefined) ? e.detail.index : e.currentTarget.dataset.index;`; if (content.indexOf(oldFn) === -1) { console.log('Could not find exact function match. Trying flexible match...'); // regex fallback content = content.replace( /revealNext:\s*function\s*\(e\)\s*\{\s*const\s*index\s*=\s*e\.currentTarget\.dataset\.index;/g, `revealNext: function (e) { const index = (e.detail && e.detail.index !== undefined) ? e.detail.index : e.currentTarget.dataset.index;` ); } else { content = content.replace(oldFn, newFn); } // Add viewCardDetail if missing if (content.indexOf('viewCardDetail:') === -1) { const insertPoint = ` } }, // --- 展示解读内容 ---`; // Find the end of revealNext and insert viewCardDetail // revealNext ends, usually followed by showInterpretation const showInterp = ` // --- 展示解读内容 ---`; const newMethod = ` // 查看已翻开牌的详情 viewCardDetail: function(e) { const index = (e.detail && e.detail.index !== undefined) ? e.detail.index : e.currentTarget.dataset.index; const card = this.data.drawnCards[index]; if (card) { wx.showToast({ title: card.name + (card.isReversed ? '(逆)' : ''), icon: 'none', duration: 1500 }); } }, // --- 展示解读内容 ---`; content = content.replace(showInterp, newMethod); } fs.writeFileSync(path, content, 'utf8'); console.log('Successfully updated index.js functions'); } catch (e) { console.error(e); }