PageSourceSearch

https://beyondc60.com/wp-content/plugins/woo-coupon-usage-pro/js/qr-code-generator.js?ver=1.0.0

js beyondc60.com collected 2026-10-04 02:39:37 UTC 10,062 bytes, 264 lines download raw bytes

1jQuery(document).ready(function($) {
2
3    function makeCode(container) {
4        if (typeof QRCode === 'undefined') {
5            console.log("QRCode library not loaded");
6            return;
7        }
8
9        var id = container.data('id');
10        var id2 = container.data('id2');
11        var qrColor = $('#wcu-qr-code-color-picker' + id).val();
12        var qrContainerId = "showqrcode" + id;
13        
14        $("#" + qrContainerId).html("");
15
16        var qrcode = new QRCode(qrContainerId, {
17            text: "",
18            width: 250,
19            height: 250,
20            colorDark: qrColor,
21            colorLight: "#ffffff",
22            correctLevel: QRCode.CorrectLevel.H
23        });
24
25        var referralurl = "";
26        if (id2 === "p1") {
27            referralurl = $.trim($('#p1short').text());
28            if (!referralurl) {
29                referralurl = $.trim($('#p1').text());
30            }
31        } else {
32            referralurl = $.trim($('#' + id2).text());
33        }
34
35        referralurl = decodeURIComponent(encodeURIComponent(referralurl));
36
37        qrcode.makeCode(referralurl);
38
39        // Logo logic
40        var logoEnable = container.data('logo-enable');
41        var logoUrl = container.data('logo-url');
42
43        if (logoEnable && logoUrl) {
44            // The qrcode library draws to <canvas> synchronously but only sets
45            // the paired <img>.src after an async data-URI probe (_safeSetDataURI).
46            // On mobile that probe can take longer than any fixed timeout, so we
47            // read the canvas directly — it is always available immediately.
48            var qrCanvas = $('#' + qrContainerId + ' canvas')[0];
49            var qrImg   = $('#' + qrContainerId + ' img')[0];
50
51            if (qrCanvas) {
52                var compositeCanvas = document.createElement('canvas');
53                var ctx = compositeCanvas.getContext('2d');
54                compositeCanvas.width  = 250;
55                compositeCanvas.height = 250;
56
57                // Draw QR from the synchronously-painted canvas
58                ctx.drawImage(qrCanvas, 0, 0, 250, 250);
59
60                var logoSize  = 50;
61                var clearSize = 50;
62                var x = (compositeCanvas.width  - clearSize) / 2;
63                var y = (compositeCanvas.height - clearSize) / 2;
64
65                ctx.fillStyle = '#ffffff';
66                ctx.fillRect(x, y, clearSize, clearSize);
67
68                ctx.strokeStyle = '#000000';
69                ctx.lineWidth = 1;
70                ctx.strokeRect(x, y, clearSize, clearSize);
71
72                var logo = new Image();
73                logo.crossOrigin = 'anonymous';
74                logo.onload = function() {
75                    var logoX = (compositeCanvas.width  - logoSize) / 2;
76                    var logoY = (compositeCanvas.height - logoSize) / 2;
77                    ctx.drawImage(logo, logoX, logoY, logoSize, logoSize);
78
79                    try {
80                        var dataUrl = compositeCanvas.toDataURL('image/png');
81                        if (qrImg) {
82                            qrImg.src = dataUrl;
83                            qrImg.style.display = 'block';
84                        }
85                        // Store for download fallback
86                        $('#' + qrContainerId).data('composite-canvas', compositeCanvas);
87                    } catch (e) {
88                        console.log('QR logo canvas error: ' + e);
89                    }
90                    if (qrImg) {
91                        qrImg.setAttribute('data-logo-added', 'true');
92                    }
93                };
94                logo.onerror = function() {
95                    // Logo failed to load — unblock download so it still works
96                    if (qrImg) {
97                        qrImg.setAttribute('data-logo-added', 'true');
98                    }
99                };
100                logo.src = logoUrl;
101            }
102        }
103    }
104
105    // Event delegation for Generate button
106    $(document).on('click', '.wcusage_landing_qr_show', function(e) {
107        e.preventDefault();
108        var id = $(this).data('id');
109        var container = $(".display-qr-code" + id);
110        
111        if (container.is(":visible")) {
112            container.hide();
113        } else {
114            $("#showqrcode" + id).show();
115            container.css("opacity", 0).css("display", "inline-block").animate({ opacity: 1 }, 500);
116            $("#wcu-download-qr" + id).show();
117            
118            setTimeout(function() {
119                try {
120                    makeCode(container);
121                } catch (err) {
122                    console.log("Error generating QR code: " + err);
123                }
124            }, 50);
125        }
126    });
127
128    // Event delegation for Color Picker
129    $(document).on('change', '.wcu-qr-code-color-picker-input', function() {
130        var id = $(this).attr('id').replace('wcu-qr-code-color-picker', '');
131        var container = $(".display-qr-code" + id);
132        setTimeout(function() {
133            makeCode(container);
134        }, 200);
135    });
136
137    // Event delegation for Campaign Change
138    $(document).on('change', '#wcu-referral-campaign', function() {
139        $('.wcu-display-qr-code:visible').each(function() {
140            var container = $(this);
141            setTimeout(function() {
142                makeCode(container);
143            }, 200);
144        });
145    });
146
147    // Event delegation for Custom URL Input
148    $(document).on('input', '.wcusage_custom_ref_url', function() {
149        $('.wcu-display-qr-code:visible').each(function() {
150            var container = $(this);
151            setTimeout(function() {
152                makeCode(container);
153            }, 200);
154        });
155    });
156
157    // Event delegation for Short URL Generate
158    $(document).on('click', '#generate-short-url', function() {
159        $('.wcu-display-qr-code:visible').each(function() {
160            var container = $(this);
161            setTimeout(function() {
162                makeCode(container);
163            }, 2500);
164        });
165    });
166
167    // Download Functionality
168    window.wcusage_downloadQR = function(id) {
169        var container  = $(".display-qr-code" + id);
170        var title      = container.data('title');
171        var logoEnable = container.data('logo-enable');
172        var logoUrl    = container.data('logo-url');
173
174        var img    = $('#showqrcode' + id + ' img')[0];
175        var canvas = $('#showqrcode' + id + ' canvas')[0];
176
177        if (logoEnable && logoUrl) {
178            // Poll for the logo composite, but give up after 3 s so the
179            // button never silently hangs forever (was: no timeout at all).
180            var attempts    = 0;
181            var maxAttempts = 30; // 30 × 100 ms = 3 s
182            var checkLogo   = function() {
183                if (img && img.getAttribute('data-logo-added') === 'true') {
184                    downloadImage(img.src, title);
185                } else if (attempts >= maxAttempts) {
186                    // Timed out — fall back to whatever is available
187                    downloadFromSource(img, canvas, title);
188                } else {
189                    attempts++;
190                    setTimeout(checkLogo, 100);
191                }
192            };
193            checkLogo();
194        } else {
195            downloadFromSource(img, canvas, title);
196        }
197    };
198
199    // Download from img.src, falling back to the raw canvas if src is empty
200    function downloadFromSource(img, canvas, title) {
201        if (img && img.src && img.src !== window.location.href) {
202            downloadImage(img.src, title);
203        } else if (canvas) {
204            try {
205                downloadImage(canvas.toDataURL('image/png'), title);
206            } catch (e) {
207                console.log('QR download canvas error: ' + e);
208            }
209        }
210    }
211
212    // Convert data URL → Blob → object URL so mobile browsers download the
213    // file as a proper PNG instead of saving it as "<name>.png.html".
214    function downloadImage(dataUrl, title) {
215        if (!dataUrl || dataUrl === window.location.href) {
216            console.log('QR download: no image data available');
217            return;
218        }
219        var imgName = 'qr-' + title;
220        try {
221            var parts = dataUrl.split(',');
222            var mime  = parts[0].match(/:(.*?);/)[1];
223            var raw   = atob(parts[1]);
224            var n     = raw.length;
225            var bytes = new Uint8Array(n);
226            while (n--) { bytes[n] = raw.charCodeAt(n); }
227            var blob    = new Blob([bytes], { type: mime });
228            var blobUrl = URL.createObjectURL(blob);
229            var a = document.createElement('a');
230            a.href     = blobUrl;
231            a.download = imgName + '.png';
232            document.body.appendChild(a);
233            a.click();
234            document.body.removeChild(a);
235            setTimeout(function() { URL.revokeObjectURL(blobUrl); }, 1000);
236        } catch (e) {
237            // Last-resort fallback for very old browsers
238            var a = document.createElement('a');
239            a.href     = dataUrl;
240            a.download = imgName + '.png';
241            document.body.appendChild(a);
242            a.click();
243            document.body.removeChild(a);
244        }
245    }
246    
247    // Attach click handler for download buttons
248    $(document).on('click', '.wcu-download-qr', function() {
249        var id = $(this).attr('id').replace('wcu-download-qr', '');
250        wcusage_downloadQR(id);
251    });
252
253    // Auto-generate QR codes for shortcode
254    $('.wcu-display-qr-code-shortcode').each(function() {
255        var container = $(this);
256        var id = container.data('id');
257        $("#showqrcode" + id).show();
258        $("#wcu-download-qr" + id).show();
259        setTimeout(function() {
260            makeCode(container);
261        }, 200);
262    });
263
264});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.