-
-
Notifications
You must be signed in to change notification settings - Fork 65
feat: implement QR code extension using dynamic CDN loading #217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
amankv1234
wants to merge
4
commits into
AOSSIE-Org:main
Choose a base branch
from
amankv1234:feature/qr-code-extension
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3106856
feat: implement QR code extension with dynamic CDN loading
amankv1234 1eb67eb
fix: address CodeRabbit review comments on QR extension
amankv1234 f691471
fix: address further CodeRabbit review comments
amankv1234 b57254d
fix: externalize QR string labels to support localization
amankv1234 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,243 @@ | ||
| /** | ||
| * SocialShareButton QR Code Extension | ||
| * Dynamically loads Kazuhiko Arase's qrcode-generator from CDN | ||
| */ | ||
|
|
||
| (function () { | ||
| /** | ||
| * Shared bootstrap error helper for the QR extension. | ||
| * Keeps all console output in one place so it can be easily | ||
| * swapped for a project-level logger without touching call sites. | ||
| * | ||
| * @param {string} message | ||
| */ | ||
| function _qrWarn(message) { | ||
| if (typeof console !== "undefined" && typeof console.warn === "function") { | ||
| console.warn("[SocialShareButton QR] " + message); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** | ||
| * Cached Promise for the qrcode-generator CDN load. | ||
| * Guarantees only one <script> tag is ever injected regardless of | ||
| * how many QR clicks happen before the first load completes. | ||
| * @type {Promise<void>|null} | ||
| */ | ||
| var _generatorPromise = null; | ||
|
|
||
| /** | ||
| * Returns a Promise that resolves once window.qrcode is available. | ||
| * If the library is already present (e.g. self-hosted) it resolves immediately. | ||
| * Subsequent calls return the same cached Promise. | ||
| * | ||
| * @returns {Promise<void>} | ||
| */ | ||
| function getQRCodeGenerator() { | ||
| if (_generatorPromise) return _generatorPromise; | ||
|
|
||
| if (typeof window.qrcode !== "undefined") { | ||
| _generatorPromise = Promise.resolve(); | ||
| return _generatorPromise; | ||
| } | ||
|
|
||
| _generatorPromise = new Promise(function (resolve, reject) { | ||
| var script = document.createElement("script"); | ||
| script.src = "https://cdn.jsdelivr.net/npm/qrcode-generator@1.4.4/qrcode.min.js"; | ||
| script.onload = resolve; | ||
| script.onerror = function () { | ||
| // Reset so a retry (e.g. after fixing CSP) can attempt the load again | ||
| _generatorPromise = null; | ||
| _qrWarn( | ||
| "Failed to load qrcode-generator from CDN (https://cdn.jsdelivr.net). " + | ||
| "Check your network connection or Content Security Policy. " + | ||
| "To self-host, load qrcode.min.js before social-share-button-qr.js." | ||
| ); | ||
| reject(new Error("qrcode-generator CDN load failed")); | ||
| }; | ||
| document.head.appendChild(script); | ||
| }); | ||
|
|
||
| return _generatorPromise; | ||
| } | ||
|
|
||
| function applyQRPatch() { | ||
| if (typeof window === "undefined" || !window.SocialShareButton) { | ||
| _qrWarn("SocialShareButton core must be loaded before the QR extension."); | ||
| return; | ||
| } | ||
|
|
||
| // Guard against double-patching | ||
| if (window.SocialShareButton._qrPatched) return; | ||
| window.SocialShareButton._qrPatched = true; | ||
|
|
||
| var originalShare = window.SocialShareButton.prototype.share; | ||
| var originalCloseModal = window.SocialShareButton.prototype.closeModal; | ||
|
|
||
| window.SocialShareButton.prototype.share = function (platform) { | ||
| if (platform === "qrcode") { | ||
| var self = this; | ||
|
|
||
| this._emit("social_share_click", "share", { platform: platform }); | ||
|
|
||
| // Show a pending/disabled state on the QR button while the library loads | ||
| var qrBtn = this.modal | ||
| ? this.modal.querySelector('[data-platform="qrcode"]') | ||
| : null; | ||
| if (qrBtn) { | ||
| qrBtn.disabled = true; | ||
| qrBtn.setAttribute("aria-busy", "true"); | ||
| } | ||
|
|
||
| getQRCodeGenerator() | ||
| .then(function () { | ||
| var rendered = self.renderQRPanel(); | ||
| // Only emit success and invoke callback after rendering succeeds | ||
| if (rendered !== false) { | ||
| self._emit("social_share_success", "share", { platform: platform }); | ||
| if (self.options.onShare) { | ||
| self.options.onShare(platform, self.options.url); | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| }) | ||
| .catch(function () { | ||
| // CDN failed — warning already logged inside getQRCodeGenerator | ||
| }) | ||
| .then(function () { | ||
| // Restore button regardless of success or failure (acts as .finally) | ||
| if (qrBtn) { | ||
| qrBtn.disabled = false; | ||
| qrBtn.removeAttribute("aria-busy"); | ||
| } | ||
| }); | ||
|
|
||
| return; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // Delegate all other platforms to the original handler | ||
| return originalShare.call(this, platform); | ||
| }; | ||
|
|
||
| window.SocialShareButton.prototype.renderQRPanel = function () { | ||
| if (!this.modal) return false; | ||
|
|
||
| // Do not render twice | ||
| if (this.modal.querySelector(".social-share-qr-panel")) return; | ||
|
|
||
| if (typeof window.qrcode === "undefined") { | ||
| _qrWarn("qrcode-generator is not available. The QR panel cannot be rendered."); | ||
| return false; | ||
| } | ||
|
|
||
| // --- Generate QR data --- | ||
| var typeNumber = 0; // 0 = auto-detect | ||
| var errorCorrectionLevel = "M"; | ||
| var qr = window.qrcode(typeNumber, errorCorrectionLevel); | ||
| qr.addData(this.options.url); | ||
| qr.make(); | ||
|
|
||
| var moduleCount = qr.getModuleCount(); | ||
| var cellSize = Math.max(3, Math.floor(180 / moduleCount)); | ||
| var margin = 4; | ||
| var size = moduleCount * cellSize + margin * 2 * cellSize; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| // --- Build DOM --- | ||
| var qrPanel = document.createElement("div"); | ||
| qrPanel.className = "social-share-qr-panel"; | ||
|
|
||
| var title = document.createElement("h4"); | ||
| title.textContent = "Scan QR Code"; | ||
|
|
||
| var canvas = document.createElement("canvas"); | ||
| canvas.className = "social-share-qr-canvas"; | ||
| canvas.width = size; | ||
| canvas.height = size; | ||
|
|
||
| var ctx = canvas.getContext("2d"); | ||
|
|
||
| // White background | ||
| ctx.fillStyle = "#ffffff"; | ||
| ctx.fillRect(0, 0, size, size); | ||
|
|
||
| // Dark modules | ||
| ctx.fillStyle = "#000000"; | ||
| for (var row = 0; row < moduleCount; row++) { | ||
| for (var col = 0; col < moduleCount; col++) { | ||
| if (qr.isDark(row, col)) { | ||
| ctx.fillRect( | ||
| (col + margin) * cellSize, | ||
| (row + margin) * cellSize, | ||
| cellSize, | ||
| cellSize | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| var downloadBtn = document.createElement("button"); | ||
| downloadBtn.className = "social-share-qr-download"; | ||
| downloadBtn.textContent = "Download QR"; | ||
|
|
||
| var self = this; | ||
| var downloadHandler = function () { | ||
| var dataUrl = canvas.toDataURL("image/png"); | ||
| var a = document.createElement("a"); | ||
| a.href = dataUrl; | ||
| a.download = "share-qrcode.png"; | ||
| document.body.appendChild(a); | ||
| a.click(); | ||
| document.body.removeChild(a); | ||
| }; | ||
|
|
||
| downloadBtn.addEventListener("click", downloadHandler); | ||
| // Register in central listener list so destroy() cleans it up | ||
| this.addEventListener(downloadBtn, "click", downloadHandler); | ||
|
|
||
| this._qrDownloadHandler = downloadHandler; | ||
| this._qrDownloadBtn = downloadBtn; | ||
|
|
||
| qrPanel.appendChild(title); | ||
| qrPanel.appendChild(canvas); | ||
| qrPanel.appendChild(downloadBtn); | ||
|
|
||
| // Insert right after the platforms row | ||
| var platformsContainer = this.modal.querySelector(".social-share-platforms"); | ||
| if (platformsContainer && platformsContainer.parentNode) { | ||
| platformsContainer.parentNode.insertBefore(qrPanel, platformsContainer.nextSibling); | ||
| } else { | ||
| var content = this.modal.querySelector(".social-share-modal-content"); | ||
| if (content) content.appendChild(qrPanel); | ||
| } | ||
| }; | ||
|
|
||
| window.SocialShareButton.prototype.closeModal = function () { | ||
| if (this.modal) { | ||
| var qrPanel = this.modal.querySelector(".social-share-qr-panel"); | ||
| if (qrPanel) { | ||
| if (this._qrDownloadBtn && this._qrDownloadHandler) { | ||
| this._qrDownloadBtn.removeEventListener("click", this._qrDownloadHandler); | ||
| // Purge from central registry | ||
| this.listeners = this.listeners.filter( | ||
| function (l) { return l.handler !== this._qrDownloadHandler; }, | ||
| this | ||
| ); | ||
| this._qrDownloadBtn = null; | ||
| this._qrDownloadHandler = null; | ||
| } | ||
| qrPanel.remove(); | ||
| } | ||
| } | ||
| return originalCloseModal.call(this); | ||
| }; | ||
| } // end applyQRPatch | ||
|
|
||
| // Patch prototype immediately — CDN load is deferred to first QR click. | ||
| // Guard against SSR environments (Next.js, Nuxt, etc.) where window/document | ||
| // are undefined at import time. | ||
| if (typeof window !== "undefined" && typeof document !== "undefined") { | ||
| if (document.readyState === "loading") { | ||
| document.addEventListener("DOMContentLoaded", applyQRPatch); | ||
| } else { | ||
| applyQRPatch(); | ||
| } | ||
| } | ||
| })(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.