Plugin Directory

Changeset 3456951


Ignore:
Timestamp:
02/09/2026 10:33:03 AM (8 weeks ago)
Author:
wpcompany
Message:

Correctly decodes Base64 UTF-8 strings

Location:
media-focus-point/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • media-focus-point/trunk/readme.txt

    r3432587 r3456951  
    110110== Changelog ==
    111111
     112= 2.0.5 =
     113* Correctly decodes Base64 UTF-8 strings for opening in modal (Example: ExampleImagewithCopyright©.png)
     114
    112115= 2.0.4 =
    113116* Weird bug when setting focus point for Featured Image or ACF Image fields
  • media-focus-point/trunk/script.js

    r3432587 r3456951  
    110110}
    111111
     112// Decodes a base64 string and handles UTF-8 characters correctly
     113function b64DecodeUTF8(str) {
     114    try {
     115        const binString = atob(str);
     116        const bytes = new Uint8Array(binString.length);
     117        for (let i = 0; i < binString.length; i++) {
     118            bytes[i] = binString.charCodeAt(i);
     119        }
     120        return new TextDecoder('utf-8').decode(bytes);
     121    } catch (e) {
     122        console.error("Media Focus Point: Error decoding media tag:", e);
     123        return atob(str); // Fallback to standard atob
     124    }
     125}
     126
    112127function waitForMediaLoad(media) {
    113     return new Promise((resolve) => {
     128    return new Promise((resolve, reject) => {
    114129        if (media.tagName.toLowerCase() === "video") {
    115130            if (media.readyState >= 2) {
     
    117132            } else {
    118133                media.onloadeddata = () => resolve(media);
     134                media.onerror = () => reject(new Error("Video failed to load"));
    119135            }
    120136        } else {
    121             if (media.complete) {
     137            if (media.complete && media.naturalWidth !== 0) {
    122138                resolve(media);
    123139            } else {
    124140                media.onload = () => resolve(media);
    125             }
    126         }
     141                media.onerror = () => reject(new Error("Image failed to load"));
     142            }
     143        }
     144
     145        // Add a timeout to prevent absolute hanging
     146        setTimeout(() => reject(new Error("Media load timeout")), 5000);
    127147    });
    128148}
     
    156176    mediaPreviewContainer.classList.add('wpcmfp-media-preview-container');
    157177    // Fetch data-attribute data-media-tag, which should contain a base64 encoded string of the media tag
    158     mediaPreviewContainer.innerHTML = atob(hiddenInput.dataset.mediaTag);
     178    mediaPreviewContainer.innerHTML = b64DecodeUTF8(hiddenInput.dataset.mediaTag);
    159179    // Save pin to add back again later
    160180    const pin = mediaContainer.querySelector('.wpcmfp-pin');
     
    167187
    168188    replaceCustomVideoElements()
    169     document.querySelectorAll(".wpcmfp-media-frame-content,.wpcmfp-media-sidebar").forEach(el => el.classList.add('show'));
     189    document.querySelectorAll(".wpcmfp-media-frame-content,.wpcmfp-media-sidebar").forEach(el => el.classList.add('wpcmfp-show'));
    170190    document.querySelectorAll(".wpcmfp-media-toolbar,.wpcmfp-media-menu-item").forEach(el => el.style.zIndex = 0);
    171191    set_bg_values();
     
    181201    }
    182202
    183     await waitForMediaLoad(media);
     203    try {
     204        await waitForMediaLoad(media);
     205    } catch (error) {
     206        console.warn("Media load error, proceeding anyway:", error);
     207    }
    184208    const rect = media.getBoundingClientRect();
    185209    containerHt = container.clientHeight;
Note: See TracChangeset for help on using the changeset viewer.