dep.js 1.18 KB
Newer Older
李维's avatar
李维 committed
1 2
const actionName = "getDPEData";

李维's avatar
李维 committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16
const commonPostMessage = function (messageObj) {
    const obj = { ...messageObj, urlParams: window.location.search, origin: "template" }
    window.parent.postMessage(obj, '*');
};

export const getDPEData = async function () {
    return new Promise((resolve, reject) => {
        let processed = false;
        let timeoutId = setTimeout(() => {
            if (!processed) {
                window.removeEventListener("message", eventCallback);
                reject("timeout")
            }
        }, 5000);
李维's avatar
李维 committed
17

李维's avatar
李维 committed
18
        const eventCallback = (e) => {
李维's avatar
李维 committed
19 20 21
            if(e.data.action !== actionName) {
                return;
            }
李维's avatar
李维 committed
22 23 24
            processed = true;
            window.removeEventListener("message", eventCallback);
            clearTimeout(timeoutId);
李维's avatar
李维 committed
25
            try {
李维's avatar
李维 committed
26 27
                const data = JSON.parse(e.data.data);
                resolve(data.data)
李维's avatar
李维 committed
28 29
            } catch (error) {
                console.error("Error parsing DPE data", error);
李维's avatar
李维 committed
30
                resolve(null)
李维's avatar
李维 committed
31
            }
李维's avatar
李维 committed
32
        }
李维's avatar
李维 committed
33
        commonPostMessage({ action: actionName, data: window.location.search }); 
李维's avatar
李维 committed
34 35 36
        window.addEventListener("message", eventCallback)
    })
}