当前位置:迷你笔记 » 技术 » Shopify虚拟退款油猴代码

Shopify虚拟退款油猴代码

// ==UserScript==
// @name         Shopify Virtual Full Refund
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  Replace content on the page with specified HTML, insert new elements, extract and format date, and fetch order details
// @author       MH
// @match        https://admin.shopify.com/store/*/orders/
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 全局变量定义
    let order_date = '';
    let newamount = '';
    let containsUnfulfilled = false;

    // 添加 CSS 规则
    const style = document.createElement('style');
		style.textContent = `
	.Polaris-LegacyCard .Polaris-BlockStack .Polaris-InlineGrid {
		--pc-inline-grid-gap-xs: unset !important;
	}
    `;
    document.head.appendChild(style);

    // 创建悬浮按钮
    const button = document.createElement('button');
    button.style.position = 'fixed';
    button.style.bottom = '10px';
    button.style.right = '30px';
    button.style.zIndex = '1000';
    button.style.padding = '10px';
    button.style.backgroundColor = 'rgb(0, 123, 255)';
    button.style.color = 'rgb(255, 255, 255)';
    button.style.border = 'none';
    button.style.borderRadius = '5px';
    button.style.cursor = 'pointer';
    button.style.opacity = '0.75';
    button.style.display = 'none'; // 初始隐藏
    button.textContent = 'Full Refund';
    document.body.appendChild(button);

    // 检测鼠标位置
    function handleMouseMove(event) {
        const mouseX = event.clientX;
        const mouseY = event.clientY;
        const windowWidth = window.innerWidth;
        const windowHeight = window.innerHeight;

        // 判断鼠标是否在右下角区域
        if (mouseX > windowWidth - 100 && mouseY > windowHeight - 100) {
            button.style.display = 'block';
        } else {
            button.style.display = 'none';
        }
    }

    // 绑定鼠标移动事件
    document.addEventListener('mousemove', handleMouseMove);

    // 按钮点击事件
    button.addEventListener('click', async function() {
        await ensurePageLoaded(); // 确保页面内容完全加载
        await extractAndFormatDate(); // 提取并格式化日期
        await fetchOrderDetails(); // 获取订单详情


        // 查找内容为 "Total" 的元素,并替换其上级的上级的同级 div
        const totalElements = document.querySelectorAll('span.Polaris-Text--root.Polaris-Text--bodyMd.Polaris-Text--semibold');
        totalElements.forEach(element => {
            if (element.textContent.trim() === 'Total') {
                const grandparentDiv = element.closest('div').parentElement.parentElement;
                if (grandparentDiv) {
                    const siblingDiv = grandparentDiv.querySelector('div:not(.Polaris-Text--root.Polaris-Text--bodyMd.Polaris-Text--semibold)');
                    if (siblingDiv) {
                        siblingDiv.innerHTML = `
                            <div class="Polaris-BlockStack" style="--pc-block-stack-order: column; --pc-block-stack-gap-xs: unset;"><div class="Polaris-InlineGrid" style="--pc-inline-grid-grid-template-columns-xs: 1fr auto; --pc-inline-grid-gap-xs: unset;"><div class="Polaris-InlineStack" style="--pc-inline-stack-block-align: baseline; --pc-inline-stack-wrap: nowrap; --pc-inline-stack-gap-xs: unset; --pc-inline-stack-flex-direction-xs: row;"><span class="Polaris-Text--root Polaris-Text--bodyMd Polaris-Text--semibold">Total</span></div><div><div class="Polaris-InlineStack" style="--pc-inline-stack-align: space-between; --pc-inline-stack-block-align: center; --pc-inline-stack-wrap: nowrap; --pc-inline-stack-gap-xs: unset; --pc-inline-stack-flex-direction-xs: row;"><span class="Polaris-Text--root Polaris-Text--bodyMd Polaris-Text--semibold"></span><span class="Polaris-Text--root Polaris-Text--bodyMd Polaris-Text--semibold">${currencySymbol}0.00</span></div></div></div></div>
                        `;
                    }
                }
            }
        });

        // 检查是否存在内容包含 "Unfulfilled" 的元素
        const textElements = document.querySelectorAll('span.Polaris-Text--root.Polaris-Text--bodySm');
        textElements.forEach(element => {
            if (element.textContent.includes('Unfulfilled')) {
                containsUnfulfilled = true;
            }
        });

        const fulfilledContent = `
            <div class="Polaris-InlineStack" style="--pc-inline-stack-wrap: wrap; --pc-inline-stack-gap-xs: var(--p-space-100); --pc-inline-stack-flex-direction-xs: row;"><span class="Polaris-Badge"><span class="Polaris-Badge__Icon"><span class="Polaris-Icon"><span class="Polaris-Text--root Polaris-Text--visuallyHidden">Complete</span><span class="Polaris-Icon__SvgSmScreen"><svg viewBox="0 0 20 20"><path d="M6 10c0-.93 0-1.395.102-1.776a3 3 0 0 1 2.121-2.122C8.605 6 9.07 6 10 6c.93 0 1.395 0 1.776.102a3 3 0 0 1 2.122 2.122C14 8.605 14 9.07 14 10s0 1.395-.102 1.777a3 3 0 0 1-2.122 2.12C11.395 14 10.93 14 10 14s-1.395 0-1.777-.102a3 3 0 0 1-2.12-2.121C6 11.395 6 10.93 6 10Z"></path></svg></span><span class="Polaris-Icon__SvgLgScreen"><svg viewBox="0 0 20 20"><path d="M6 10c0-.93 0-1.395.102-1.776a3 3 0 0 1 2.121-2.122C8.605 6 9.07 6 10 6c.93 0 1.395 0 1.776.102a3 3 0 0 1 2.122 2.122C14 8.605 14 9.07 14 10s0 1.395-.102 1.777a3 3 0 0 1-2.122 2.12C11.395 14 10.93 14 10 14s-1.395 0-1.777-.102a3 3 0 0 1-2.12-2.121C6 11.395 6 10.93 6 10Z"></path></svg></span></span></span><span class="Polaris-Text--root Polaris-Text--bodySm">Refunded</span></span><span class="Polaris-Badge"><span class="Polaris-Badge__Icon"><span class="Polaris-Icon"><span class="Polaris-Text--root Polaris-Text--visuallyHidden">Complete</span><span class="Polaris-Icon__SvgSmScreen"><svg viewBox="0 0 20 20"><path d="M6 10c0-.93 0-1.395.102-1.776a3 3 0 0 1 2.121-2.122C8.605 6 9.07 6 10 6c.93 0 1.395 0 1.776.102a3 3 0 0 1 2.122 2.122C14 8.605 14 9.07 14 10s0 1.395-.102 1.777a3 3 0 0 1-2.122 2.12C11.395 14 10.93 14 10 14s-1.395 0-1.777-.102a3 3 0 0 1-2.12-2.121C6 11.395 6 10.93 6 10Z"></path></svg></span><span class="Polaris-Icon__SvgLgScreen"><svg viewBox="0 0 20 20"><path d="M6 10c0-.93 0-1.395.102-1.776a3 3 0 0 1 2.121-2.122C8.605 6 9.07 6 10 6c.93 0 1.395 0 1.776.102a3 3 0 0 1 2.122 2.122C14 8.605 14 9.07 14 10s0 1.395-.102 1.777a3 3 0 0 1-2.122 2.12C11.395 14 10.93 14 10 14s-1.395 0-1.777-.102a3 3 0 0 1-2.12-2.121C6 11.395 6 10.93 6 10Z"></path></svg></span></span></span><span class="Polaris-Text--root Polaris-Text--bodySm">Fulfilled</span></span><span class="Polaris-Badge"><span class="Polaris-Text--root Polaris-Text--bodySm">Archived</span></span></div>
        `;

        const infulfilledContent = `
            <div class="Polaris-InlineStack" style="--pc-inline-stack-wrap: wrap; --pc-inline-stack-gap-xs: var(--p-space-100); --pc-inline-stack-flex-direction-xs: row;"><span class="Polaris-Badge"><span class="Polaris-Badge__Icon"><span class="Polaris-Icon"><span class="Polaris-Text--root Polaris-Text--visuallyHidden">Complete</span><span class="Polaris-Icon__SvgSmScreen"><svg viewBox="0 0 20 20"><path d="M6 10c0-.93 0-1.395.102-1.776a3 3 0 0 1 2.121-2.122C8.605 6 9.07 6 10 6c.93 0 1.395 0 1.776.102a3 3 0 0 1 2.122 2.122C14 8.605 14 9.07 14 10s0 1.395-.102 1.777a3 3 0 0 1-2.122 2.12C11.395 14 10.93 14 10 14s-1.395 0-1.777-.102a3 3 0 0 1-2.12-2.121C6 11.395 6 10.93 6 10Z"></path></svg></span><span class="Polaris-Icon__SvgLgScreen"><svg viewBox="0 0 20 20"><path d="M6 10c0-.93 0-1.395.102-1.776a3 3 0 0 1 2.121-2.122C8.605 6 9.07 6 10 6c.93 0 1.395 0 1.776.102a3 3 0 0 1 2.122 2.122C14 8.605 14 9.07 14 10s0 1.395-.102 1.777a3 3 0 0 1-2.122 2.12C11.395 14 10.93 14 10 14s-1.395 0-1.777-.102a3 3 0 0 1-2.12-2.121C6 11.395 6 10.93 6 10Z"></path></svg></span></span></span><span class="Polaris-Text--root Polaris-Text--bodySm">Refunded</span></span><span class="Polaris-Badge Polaris-Badge--toneAttention"><span class="Polaris-Badge__Icon"><span class="Polaris-Icon"><span class="Polaris-Text--root Polaris-Text--visuallyHidden">Attention Incomplete</span><span class="Polaris-Icon__SvgSmScreen"><svg viewBox="0 0 20 20"><path fill-rule="evenodd" d="M8.547 12.69c.183.05.443.06 1.453.06s1.27-.01 1.453-.06a1.75 1.75 0 0 0 1.237-1.237c.05-.182.06-.443.06-1.453s-.01-1.27-.06-1.453a1.75 1.75 0 0 0-1.237-1.237c-.182-.05-.443-.06-1.453-.06s-1.27.01-1.453.06A1.75 1.75 0 0 0 7.31 8.547c-.05.183-.06.443-.06 1.453s.01 1.27.06 1.453a1.75 1.75 0 0 0 1.237 1.237ZM6.102 8.224C6 8.605 6 9.07 6 10s0 1.395.102 1.777a3 3 0 0 0 2.122 2.12C8.605 14 9.07 14 10 14s1.395 0 1.777-.102a3 3 0 0 0 2.12-2.121C14 11.395 14 10.93 14 10c0-.93 0-1.395-.102-1.776a3 3 0 0 0-2.121-2.122C11.395 6 10.93 6 10 6c-.93 0-1.395 0-1.776.102a3 3 0 0 0-2.122 2.122Z"></path></svg></span><span class="Polaris-Icon__SvgLgScreen"><svg viewBox="0 0 20 20"><path fill-rule="evenodd" d="M8.547 12.69c.183.05.443.06 1.453.06s1.27-.01 1.453-.06a1.75 1.75 0 0 0 1.237-1.237c.05-.182.06-.443.06-1.453s-.01-1.27-.06-1.453a1.75 1.75 0 0 0-1.237-1.237c-.182-.05-.443-.06-1.453-.06s-1.27.01-1.453.06A1.75 1.75 0 0 0 7.31 8.547c-.05.183-.06.443-.06 1.453s.01 1.27.06 1.453a1.75 1.75 0 0 0 1.237 1.237ZM6.102 8.224C6 8.605 6 9.07 6 10s0 1.395.102 1.777a3 3 0 0 0 2.122 2.12C8.605 14 9.07 14 10 14s1.395 0 1.777-.102a3 3 0 0 0 2.12-2.121C14 11.395 14 10.93 14 10c0-.93 0-1.395-.102-1.776a3 3 0 0 0-2.121-2.122C11.395 6 10.93 6 10 6c-.93 0-1.395 0-1.776.102a3 3 0 0 0-2.122 2.122Z"></path></svg></span></span></span><span class="Polaris-Text--root Polaris-Text--bodySm">Unfulfilled</span></span><span class="Polaris-Badge"><span class="Polaris-Text--root Polaris-Text--bodySm">Archived</span></span></div>
        `;

        // 根据检查结果决定替换内容
        const firstTargetElement = document.querySelector('.Polaris-Header-Title__TitleWrapper .Polaris-Bleed .Polaris-InlineStack');
        if (firstTargetElement) {
            firstTargetElement.innerHTML = containsUnfulfilled ? infulfilledContent : fulfilledContent;
        } else {
            alert('未找到第一个指定的元素');
        }

        // 替换第二个元素的内容
        const secondTargetElement = document.querySelector('.Polaris-Page-Header__RightAlign');
        if (secondTargetElement) {
            secondTargetElement.innerHTML = `
                <div class="Polaris-Page-Header__Actions"><div class="Polaris-ActionMenu"><div class="Polaris-ActionMenu-Actions__ActionsLayoutOuter"><div class="Polaris-ActionMenu-Actions__ActionsLayoutMeasurer"><div class="Polaris-ActionMenu-SecondaryAction"><button id="Edit" class="Polaris-Button Polaris-Button--pressable Polaris-Button--variantSecondary Polaris-Button--sizeMedium Polaris-Button--textAlignCenter" type="button"><span class="Polaris-Text--root Polaris-Text--bodySm Polaris-Text--medium">Edit</span></button></div><div class="Polaris-ActionMenu-SecondaryAction"><button class="Polaris-Button Polaris-Button--pressable Polaris-Button--variantSecondary Polaris-Button--sizeMedium Polaris-Button--textAlignCenter Polaris-Button--disclosure" type="button"><span class="Polaris-Text--root Polaris-Text--bodySm Polaris-Text--medium">Print</span><span class="Polaris-Button__Icon"><span class="Polaris-Icon"><span class="Polaris-Icon__SvgSmScreen"><svg viewBox="1 1 18 18" class="Polaris-Icon__Svg" focusable="false" aria-hidden="true"><path fill-rule="evenodd" d="M5.72 8.47a.75.75 0 0 1 1.06 0l3.47 3.47 3.47-3.47a.75.75 0 1 1 1.06 1.06l-4 4a.75.75 0 0 1-1.06 0l-4-4a.75.75 0 0 1 0-1.06Z"></path></svg></span><span class="Polaris-Icon__SvgLgScreen"><svg viewBox="0 0 20 20" class="Polaris-Icon__Svg" focusable="false" aria-hidden="true"><path fill-rule="evenodd" d="M5.72 8.47a.75.75 0 0 1 1.06 0l3.47 3.47 3.47-3.47a.75.75 0 1 1 1.06 1.06l-4 4a.75.75 0 0 1-1.06 0l-4-4a.75.75 0 0 1 0-1.06Z"></path></svg></span></span></span></button></div><div class="Polaris-ActionMenu-SecondaryAction"><button class="Polaris-Button Polaris-Button--pressable Polaris-Button--variantSecondary Polaris-Button--sizeMedium Polaris-Button--textAlignCenter Polaris-Button--disclosure" type="button"><span class="Polaris-Text--root Polaris-Text--bodySm Polaris-Text--medium">More actions</span><span class="Polaris-Button__Icon"><span class="Polaris-Icon"><span class="Polaris-Icon__SvgSmScreen"><svg viewBox="1 1 18 18" class="Polaris-Icon__Svg" focusable="false" aria-hidden="true"><path fill-rule="evenodd" d="M5.72 8.47a.75.75 0 0 1 1.06 0l3.47 3.47 3.47-3.47a.75.75 0 1 1 1.06 1.06l-4 4a.75.75 0 0 1-1.06 0l-4-4a.75.75 0 0 1 0-1.06Z"></path></svg></span><span class="Polaris-Icon__SvgLgScreen"><svg viewBox="0 0 20 20" class="Polaris-Icon__Svg" focusable="false" aria-hidden="true"><path fill-rule="evenodd" d="M5.72 8.47a.75.75 0 0 1 1.06 0l3.47 3.47 3.47-3.47a.75.75 0 1 1 1.06 1.06l-4 4a.75.75 0 0 1-1.06 0l-4-4a.75.75 0 0 1 0-1.06Z"></path></svg></span></span></span></button></div><div class="Polaris-ActionMenu-SecondaryAction"><button class="Polaris-Button Polaris-Button--pressable Polaris-Button--variantSecondary Polaris-Button--sizeMedium Polaris-Button--textAlignCenter Polaris-Button--disclosure" type="button"><span class="Polaris-Text--root Polaris-Text--bodySm Polaris-Text--medium">More actions</span><span class="Polaris-Button__Icon"><span class="Polaris-Icon"><span class="Polaris-Icon__SvgSmScreen"><svg viewBox="1 1 18 18" class="Polaris-Icon__Svg" focusable="false" aria-hidden="true"><path fill-rule="evenodd" d="M5.72 8.47a.75.75 0 0 1 1.06 0l3.47 3.47 3.47-3.47a.75.75 0 1 1 1.06 1.06l-4 4a.75.75 0 0 1-1.06 0l-4-4a.75.75 0 0 1 0-1.06Z"></path></svg></span><span class="Polaris-Icon__SvgLgScreen"><svg viewBox="0 0 20 20" class="Polaris-Icon__Svg" focusable="false" aria-hidden="true"><path fill-rule="evenodd" d="M5.72 8.47a.75.75 0 0 1 1.06 0l3.47 3.47 3.47-3.47a.75.75 0 1 1 1.06 1.06l-4 4a.75.75 0 0 1-1.06 0l-4-4a.75.75 0 0 1 0-1.06Z"></path></svg></span></span></span></button></div></div><div class="Polaris-ActionMenu-Actions__ActionsLayout"><div class="Polaris-ActionMenu-SecondaryAction"><button id="Edit" class="Polaris-Button Polaris-Button--pressable Polaris-Button--variantSecondary Polaris-Button--sizeMedium Polaris-Button--textAlignCenter" type="button"><span class="Polaris-Text--root Polaris-Text--bodySm Polaris-Text--medium">Edit</span></button></div><div><div class="Polaris-ActionMenu-SecondaryAction"><button class="Polaris-Button Polaris-Button--pressable Polaris-Button--variantSecondary Polaris-Button--sizeMedium Polaris-Button--textAlignCenter Polaris-Button--disclosure" type="button" tabindex="0" aria-controls=":r1kh:" aria-owns=":r1kh:" aria-expanded="false" data-state="closed"><span class="Polaris-Text--root Polaris-Text--bodySm Polaris-Text--medium">Print</span><span class="Polaris-Button__Icon"><span class="Polaris-Icon"><span class="Polaris-Icon__SvgSmScreen"><svg viewBox="1 1 18 18" class="Polaris-Icon__Svg" focusable="false" aria-hidden="true"><path fill-rule="evenodd" d="M5.72 8.47a.75.75 0 0 1 1.06 0l3.47 3.47 3.47-3.47a.75.75 0 1 1 1.06 1.06l-4 4a.75.75 0 0 1-1.06 0l-4-4a.75.75 0 0 1 0-1.06Z"></path></svg></span><span class="Polaris-Icon__SvgLgScreen"><svg viewBox="0 0 20 20" class="Polaris-Icon__Svg" focusable="false" aria-hidden="true"><path fill-rule="evenodd" d="M5.72 8.47a.75.75 0 0 1 1.06 0l3.47 3.47 3.47-3.47a.75.75 0 1 1 1.06 1.06l-4 4a.75.75 0 0 1-1.06 0l-4-4a.75.75 0 0 1 0-1.06Z"></path></svg></span></span></span></button></div></div><div><div class="Polaris-ActionMenu-SecondaryAction"><button class="Polaris-Button Polaris-Button--pressable Polaris-Button--variantSecondary Polaris-Button--sizeMedium Polaris-Button--textAlignCenter Polaris-Button--disclosure" type="button" tabindex="0" aria-controls=":r1ki:" aria-owns=":r1ki:" aria-expanded="false" data-state="closed"><span class="Polaris-Text--root Polaris-Text--bodySm Polaris-Text--medium">More actions</span><span class="Polaris-Button__Icon"><span class="Polaris-Icon"><span class="Polaris-Icon__SvgSmScreen"><svg viewBox="1 1 18 18" class="Polaris-Icon__Svg" focusable="false" aria-hidden="true"><path fill-rule="evenodd" d="M5.72 8.47a.75.75 0 0 1 1.06 0l3.47 3.47 3.47-3.47a.75.75 0 1 1 1.06 1.06l-4 4a.75.75 0 0 1-1.06 0l-4-4a.75.75 0 0 1 0-1.06Z"></path></svg></span><span class="Polaris-Icon__SvgLgScreen"><svg viewBox="0 0 20 20" class="Polaris-Icon__Svg" focusable="false" aria-hidden="true"><path fill-rule="evenodd" d="M5.72 8.47a.75.75 0 0 1 1.06 0l3.47 3.47 3.47-3.47a.75.75 0 1 1 1.06 1.06l-4 4a.75.75 0 0 1-1.06 0l-4-4a.75.75 0 0 1 0-1.06Z"></path></svg></span></span></span></button></div></div></div></div></div><div class="Polaris-Page-Header__PaginationWrapper"><div class="Polaris-Box Polaris-Box--printHidden"><nav aria-label="Pagination" class="Polaris-Pagination"><div class="Polaris-ButtonGroup Polaris-ButtonGroup--variantSegmented" data-buttongroup-variant="segmented"><div class="Polaris-ButtonGroup__Item"><span class=""><button id="previousURL" class="Polaris-Button Polaris-Button--pressable Polaris-Button--variantSecondary Polaris-Button--sizeMedium Polaris-Button--textAlignCenter Polaris-Button--iconOnly" aria-label="Previous" type="button" tabindex="0" aria-describedby=":r1ko:" data-polaris-tooltip-activator="true"><span class="Polaris-Button__Icon"><span class="Polaris-Icon"><span class="Polaris-Icon__SvgSmScreen"><svg viewBox="1 1 18 18" class="Polaris-Icon__Svg" focusable="false" aria-hidden="true"><path fill-rule="evenodd" d="M11.764 5.204a.75.75 0 0 1 .032 1.06l-3.516 3.736 3.516 3.736a.75.75 0 1 1-1.092 1.028l-4-4.25a.75.75 0 0 1 0-1.028l4-4.25a.75.75 0 0 1 1.06-.032Z"></path></svg></span><span class="Polaris-Icon__SvgLgScreen"><svg viewBox="0 0 20 20" class="Polaris-Icon__Svg" focusable="false" aria-hidden="true"><path fill-rule="evenodd" d="M11.764 5.204a.75.75 0 0 1 .032 1.06l-3.516 3.736 3.516 3.736a.75.75 0 1 1-1.092 1.028l-4-4.25a.75.75 0 0 1 0-1.028l4-4.25a.75.75 0 0 1 1.06-.032Z"></path></svg></span></span></span></button></span></div><div class="Polaris-ButtonGroup__Item"><span class=""><button id="nextURL" class="Polaris-Button Polaris-Button--pressable Polaris-Button--variantSecondary Polaris-Button--sizeMedium Polaris-Button--textAlignCenter Polaris-Button--iconOnly" aria-label="Next" type="button" tabindex="0" aria-describedby=":r1kp:" data-polaris-tooltip-activator="true"><span class="Polaris-Button__Icon"><span class="Polaris-Icon"><span class="Polaris-Icon__SvgSmScreen"><svg viewBox="1 1 18 18" class="Polaris-Icon__Svg" focusable="false" aria-hidden="true"><path fill-rule="evenodd" d="M7.72 14.53a.75.75 0 0 1 0-1.06l3.47-3.47-3.47-3.47a.75.75 0 0 1 1.06-1.06l4 4a.75.75 0 0 1 0 1.06l-4 4a.75.75 0 0 1-1.06 0Z"></path></svg></span><span class="Polaris-Icon__SvgLgScreen"><svg viewBox="0 0 20 20" class="Polaris-Icon__Svg" focusable="false" aria-hidden="true"><path fill-rule="evenodd" d="M7.72 14.53a.75.75 0 0 1 0-1.06l3.47-3.47-3.47-3.47a.75.75 0 0 1 1.06-1.06l4 4a.75.75 0 0 1 0 1.06l-4 4a.75.75 0 0 1-1.06 0Z"></path></svg></span></span></span></button></span></div></div></nav></div></div></div>
            `;
        } else {
            alert('未找到第二个指定的元素');
        }

        // 替换第三个元素的内容,排除特定的元素
        const badgeElements = document.querySelectorAll('span.Polaris-Badge.Polaris-Badge--sizeLarge');
        badgeElements.forEach(badge => {
            if (!badge.classList.contains('Polaris-Badge--toneSuccess') && !badge.classList.contains('Polaris-Badge--toneAttention')) {
                badge.innerHTML = `
                    <span class="Polaris-Badge__Icon"><span class="Polaris-Icon"><span class="Polaris-Icon__SvgSmScreen"><svg viewBox="1 1 18 18" class="Polaris-Icon__Svg" focusable="false" aria-hidden="true"><path d="M13.25 11.25a.75.75 0 0 1-1.5 0v-.5a1.75 1.75 0 0 0-1.75-1.75h-.94l.72.72a.75.75 0 0 1-1.06 1.06l-2-2a.75.75 0 0 1 0-1.06l2-2a.75.75 0 0 1 1.06 1.06l-.72.72h.94a3.25 3.25 0 0 1 3.25 3.25v.5Z"></path><path fill-rule="evenodd" d="M4 16a1.5 1.5 0 0 0 2.615 1.003l1.135-1.26 1.135 1.26a1.5 1.5 0 0 0 2.23 0l1.135-1.26 1.135 1.26a1.5 1.5 0 0 0 2.615-1.003v-11a2.5 2.5 0 0 0-2.5-2.5h-7a2.5 2.5 0 0 0-2.5 2.5v11Zm2.5-12a1 1 0 0 0-1 1v11l1.507-1.674a1 1 0 0 1 1.486 0l1.507 1.674 1.507-1.674a1 1 0 0 1 1.486 0l1.507 1.674v-11a1 1 0 0 0-1-1h-7Z"></path></svg></span><span class="Polaris-Icon__SvgLgScreen"><svg viewBox="0 0 20 20" class="Polaris-Icon__Svg" focusable="false" aria-hidden="true"><path d="M13.25 11.25a.75.75 0 0 1-1.5 0v-.5a1.75 1.75 0 0 0-1.75-1.75h-.94l.72.72a.75.75 0 0 1-1.06 1.06l-2-2a.75.75 0 0 1 0-1.06l2-2a.75.75 0 0 1 1.06 1.06l-.72.72h.94a3.25 3.25 0 0 1 3.25 3.25v.5Z"></path><path fill-rule="evenodd" d="M4 16a1.5 1.5 0 0 0 2.615 1.003l1.135-1.26 1.135 1.26a1.5 1.5 0 0 0 2.23 0l1.135-1.26 1.135 1.26a1.5 1.5 0 0 0 2.615-1.003v-11a2.5 2.5 0 0 0-2.5-2.5h-7a2.5 2.5 0 0 0-2.5 2.5v11Zm2.5-12a1 1 0 0 0-1 1v11l1.507-1.674a1 1 0 0 1 1.486 0l1.507 1.674 1.507-1.674a1 1 0 0 1 1.486 0l1.507 1.674v-11a1 1 0 0 0-1-1h-7Z"></path></svg></span></span></span><span class="Polaris-Text--root Polaris-Text--bodySm">Refunded</span>
        `;
            }
        });

        // 在第一个 class 含有 _Section_ 的 div 元素前插入新的 div
        const firstSectionElement = Array.from(document.querySelectorAll('div')).find(div => div.className.includes('_Section_'));
        if (firstSectionElement) {
            firstSectionElement.parentNode.insertAdjacentHTML('beforebegin', ``); // 插入新的 div

            // 在第一个 class 含有 _Section_ 的 div 元素下级开始插入新的 div
            firstSectionElement.insertAdjacentHTML('afterbegin', `<div class="Polaris-BlockStack" style="--pc-block-stack-order: column; --pc-block-stack-gap-xs: unset;"><div class="Polaris-BlockStack" style="--pc-block-stack-order: column; --pc-block-stack-gap-xs: unset;"><div class="Polaris-InlineGrid" style="--pc-inline-grid-grid-template-columns-xs: repeat(1, minmax(0, 1fr)); --pc-inline-grid-grid-template-columns-sm: 140px auto; --pc-inline-grid-gap-xs: unset;"><div class="Polaris-InlineStack" style="--pc-inline-stack-block-align: baseline; --pc-inline-stack-wrap: nowrap; --pc-inline-stack-gap-xs: unset; --pc-inline-stack-flex-direction-xs: row;"><span class="Polaris-Text--root Polaris-Text--bodyMd Polaris-Text--regular">Original order</span></div><div><div class="Polaris-InlineStack" style="--pc-inline-stack-align: space-between; --pc-inline-stack-block-align: center; --pc-inline-stack-wrap: nowrap; --pc-inline-stack-gap-xs: unset; --pc-inline-stack-flex-direction-xs: row;"><span class="Polaris-Text--root Polaris-Text--bodyMd Polaris-Text--regular">${order_date}</span><span class="Polaris-Text--root Polaris-Text--bodyMd Polaris-Text--regular">${newamount}</span></div></div></div></div></div>
			<div class="Polaris-Box Polaris-Box--printHidden" style="--pc-box-padding-block-start-xs: var(--p-space-300); --pc-box-padding-block-end-xs: var(--p-space-300);"><div class="Polaris-Bleed" style="--pc-bleed-margin-inline-start-xs: var(--p-space-300); --pc-bleed-margin-inline-end-xs: var(--p-space-300);"><hr class="Polaris-Divider" style="border-block-start: var(--p-border-width-025) solid var(--p-color-border-secondary);"></div></div>`); // 插入新的 div
        } else {
            alert('未找到包含 _Section_ 的 div 元素');
        }

        // 在最后一个 class 含有 _Section_ 的 div 元素下级 div.Polaris-BlockStack 中添加两个新的 div
        const sectionElements = Array.from(document.querySelectorAll('div')).filter(div => div.className.includes('_Section_'));
        const lastSectionElement = sectionElements[sectionElements.length - 1];
        if (lastSectionElement) {
            const blockStack = lastSectionElement.querySelector('div.Polaris-BlockStack');
            if (blockStack) {
                blockStack.insertAdjacentHTML('beforeend', `<div class="Polaris-BlockStack" style="--pc-block-stack-order: column; --pc-block-stack-gap-xs: unset;"><div class="Polaris-InlineGrid" style="--pc-inline-grid-grid-template-columns-xs: repeat(1, minmax(0, 1fr)); --pc-inline-grid-grid-template-columns-sm: 140px auto; --pc-inline-grid-gap-xs: unset;"><div class="Polaris-InlineStack" style="--pc-inline-stack-block-align: baseline; --pc-inline-stack-wrap: nowrap; --pc-inline-stack-gap-xs: unset; --pc-inline-stack-flex-direction-xs: row;"><span class="Polaris-Text--root Polaris-Text--bodyMd Polaris-Text--regular">Refunded</span></div><div><div class="Polaris-InlineStack" style="--pc-inline-stack-align: space-between; --pc-inline-stack-block-align: center; --pc-inline-stack-wrap: nowrap; --pc-inline-stack-gap-xs: unset; --pc-inline-stack-flex-direction-xs: row;"><span class="Polaris-Text--root Polaris-Text--bodyMd Polaris-Text--regular">Reason: —</span><span class="Polaris-Text--root Polaris-Text--bodyMd Polaris-Text--regular">-${newamount}</span></div></div></div></div>
<div class="Polaris-BlockStack" style="--pc-block-stack-order: column; --pc-block-stack-gap-xs: unset;"><div class="Polaris-InlineGrid" style="--pc-inline-grid-grid-template-columns-xs: 1fr auto; --pc-inline-grid-gap-xs: unset;"><div class="Polaris-InlineStack" style="--pc-inline-stack-block-align: baseline; --pc-inline-stack-wrap: nowrap; --pc-inline-stack-gap-xs: unset; --pc-inline-stack-flex-direction-xs: row;"><span class="Polaris-Text--root Polaris-Text--bodyMd Polaris-Text--semibold">Net payment</span></div><div><div class="Polaris-InlineStack" style="--pc-inline-stack-align: space-between; --pc-inline-stack-block-align: center; --pc-inline-stack-wrap: nowrap; --pc-inline-stack-gap-xs: unset; --pc-inline-stack-flex-direction-xs: row;"><span class="Polaris-Text--root Polaris-Text--bodyMd Polaris-Text--semibold"></span><span class="Polaris-Text--root Polaris-Text--bodyMd Polaris-Text--semibold">${currencySymbol}0.00</span></div></div></div></div>`); // 插入两个新的 div
            } else {
                alert('未找到最后一个包含 _Section_ 的 div 元素下级 div.Polaris-BlockStack');
            }
        } else {
            alert('未找到包含 _Section_ 的 div 元素');
        }

        // 替换含有 "item" 的元素中的数字为 0,及其同级包含价格的元素中的数字为 0.00
        const itemElements = document.querySelectorAll('span.Polaris-Text--root.Polaris-Text--bodyMd.Polaris-Text--regular');
        itemElements.forEach(element => {
            if (element.textContent.includes('item')) {
                element.innerHTML = element.innerHTML.replace(/\d+/, '0'); // 替换数字为 0
                const siblingPriceElement = element.nextElementSibling;
                if (siblingPriceElement && siblingPriceElement.classList.contains('Polaris-Text--root') &&
                    siblingPriceElement.classList.contains('Polaris-Text--bodyMd') &&
                    siblingPriceElement.classList.contains('Polaris-Text--regular')) {
                    siblingPriceElement.innerHTML = siblingPriceElement.innerHTML.replace(/\d+(\.\d{2})?/, '0.00'); // 替换价格为 0.00
                }
            }
        });

        // 打印 order_date 和 newamount 以供调试
        console.log('Order Date:', order_date);
        console.log('New Amount:', newamount);
    });

    // 确保页面加载完成
    async function ensurePageLoaded() {
        return new Promise(resolve => {
            if (document.readyState === 'complete') {
                resolve();
            } else {
                window.addEventListener('load', resolve);
            }
        });
    }

    // 获取并格式化日期
    async function extractAndFormatDate() {
        return new Promise(resolve => {
            setTimeout(() => {
                const dateElement = document.querySelector('.Polaris-Page-Header__AdditionalMetaData span.Polaris-Text--root.Polaris-Text--bodySm.Polaris-Text--subdued');
                if (dateElement) {
                    const rawDateText = dateElement.textContent.trim();
                    const dateText = rawDateText.split(' at ')[0]; // 去除 " at " 及之后的内容
                    order_date = formatDate(dateText);
                } else {
                    console.warn('未找到日期元素');
                }
                resolve();
            }, 1000); // 延迟1秒以等待元素加载
        });
    }

    // 日期格式化函数
    function formatDate(dateText) {
        const dateFormats = [
            { pattern: /(\d{1,2})\/(\d{1,2})\/(\d{4})/, format: (m, d, y) => `${new Date(y, m - 1, d).toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' })}` },
            { pattern: /(\d{1,2})-(\d{1,2})-(\d{4})/, format: (m, d, y) => `${new Date(y, m - 1, d).toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' })}` },
            // 添加更多日期格式
        ];

        for (const { pattern, format } of dateFormats) {
            const match = dateText.match(pattern);
            if (match) {
                return format(...match.slice(1));
            }
        }
        return dateText; // 如果没有匹配,返回原始文本
    }

// 新增 currencySymbol 变量
let currencySymbol = '';

async function fetchOrderDetails() {
    const currentUrl = window.location.href;
    const baseUrlMatch = currentUrl.match(/https:\/\/admin\.shopify\.com\/store\/([^\/]+)\//);
    const orderIdMatch = currentUrl.match(/orders\/(\d+)/);

    if (baseUrlMatch && orderIdMatch) {
        const baseUrl = baseUrlMatch[0]; // https://admin.shopify.com/store/xxx/
        const orderId = orderIdMatch[1]; // xxxxxxx
        const apiUrl = `${baseUrl}admin/api/2024-07/orders/${orderId}.json`;

        try {
            const response = await fetch(apiUrl);
            if (!response.ok) throw new Error('网络响应不正常');

            const data = await response.json();
            const { amount, currency_code } = data.order.total_price_set.shop_money;

            // 根据 currency_code 设置符号
            switch (currency_code) {
                case 'USD':
                    currencySymbol = '$';
                    break;
                case 'EUR':
                    currencySymbol = '€';
                    break;
                case 'GBP':
                    currencySymbol = '£';
                    break;
                case 'BRL':
                    currencySymbol = 'R$';
                    break;
                default:
                    currencySymbol = currency_code;
            }

            newamount = `${currencySymbol}${amount}`;
        } catch (error) {
            console.error('获取订单详情时出错:', error);
        }
    } else {
        console.warn('无法提取 baseUrl 或 orderId');
    }
}

})();
未经允许不得转载:迷你笔记 » Shopify虚拟退款油猴代码

相关文章

评论 (0)

9 + 9 =