本文为 其乐用户(UID:960666) 发布的原创文章,转摘前请联系该用户获得许可
本帖最后由 960666 于 2025-5-22 23:00 编辑
有时候想给京东购物车商品加点备注,可用场景包括但不限于加购物车的原因、心理价位、用途备忘等等,(顺带一提,我觉得这是京东本来就应该有的功能,也给过京东反馈,然并卵)
于是在Deepseek的帮助下简单写了个Tampermonkey脚本,可以跨设备同步备注(非自动,需要借助Tampermonkey备份手动同步,可以用Dropbox、OneDrive、Google网盘、WebDAV等),因为纯粹读取的页面元素,靠商品ID识别,没有调用任何京东API和账号信息,理论上没什么安全漏洞(但还是欢迎大佬检查)。
源码如下:
- // ==UserScript==
- // @name 京东购物车商品备注
- // @namespace http://tampermonkey.net/
- // @version 0.2
- // @description 为京东购物车商品添加持久化备注标签
- // @author https://space.bilibili.com/32200385
- // @match https://cart.jd.com/cart_index*
- // @grant GM_setValue
- // @grant GM_getValue
- // ==/UserScript==
- (function() {
- 'use strict';
- // 自定义样式
- const style = document.createElement('style');
- style.textContent = `
- .remark-container {
- margin-top: 8px;
- color: #e4393c;
- font-size: 12px;
- cursor: pointer;
- padding: 2px 5px;
- border-radius: 3px;
- background-color: #fff7f8;
- border: 1px solid #ffd6d8;
- }
- .remark-container:hover {
- background-color: #ffeef0;
- }
- `;
- document.head.appendChild(style);
- // 初始化备注系统
- function initRemarks() {
- // 使用MutationObserver监听DOM变化
- const observer = new MutationObserver(mutations => {
- mutations.forEach(mutation => {
- if (mutation.addedNodes.length) {
- processItems();
- }
- });
- });
- observer.observe(document.body, {
- childList: true,
- subtree: true
- });
- // 初始处理
- processItems();
- }
- // 处理所有商品项
- function processItems() {
- document.querySelectorAll('div.item-item[data-sku]').forEach(item => {
- const sku = item.dataset.sku;
- const pPropsDiv = item.querySelector('.cell.p-props');
- // 防止重复添加
- if (!pPropsDiv.querySelector('.remark-container')) {
- // 创建备注容器
- const remarkDiv = document.createElement('div');
- remarkDiv.className = 'remark-container';
- remarkDiv.textContent = GM_getValue(`remark_${sku}`, '双击添加备注');
- // 添加双击事件
- remarkDiv.addEventListener('dblclick', function() {
- const remark = prompt('请输入商品备注:', GM_getValue(`remark_${sku}`) || '');
- if (remark !== null) {
- GM_setValue(`remark_${sku}`, remark.trim());
- this.textContent = remark.trim() || '双击添加备注';
- }
- });
- pPropsDiv.appendChild(remarkDiv);
- }
- });
- }
- // 等待购物车加载完成
- const checkExist = setInterval(() => {
- if (document.querySelector('.item-item')) {
- clearInterval(checkExist);
- initRemarks();
- }
- }, 500);
- })();
复制代码
|