-
-
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 1 commit
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
Some comments aren't visible on the classic Files Changed page.
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,176 @@ | ||
| /** | ||
| * SocialShareButton QR Code Extension | ||
| * Dynamically loads Kazuhiko Arase's qrcode-generator from CDN | ||
| */ | ||
|
|
||
| (function () { | ||
| function applyQRPatch() { | ||
| if (typeof window === "undefined" || !window.SocialShareButton) { | ||
| console.warn("SocialShareButton core must be loaded before QR extension"); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| 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") { | ||
| this._emit("social_share_click", "share", { platform }); | ||
| this.renderQRPanel(); | ||
| this._emit("social_share_success", "share", { platform }); | ||
|
|
||
| if (this.options.onShare) { | ||
| this.options.onShare(platform, this.options.url); | ||
| } | ||
| 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; | ||
|
|
||
| // Do not render twice | ||
| if (this.modal.querySelector(".social-share-qr-panel")) return; | ||
|
|
||
| if (typeof window.qrcode === "undefined") { | ||
| console.error("qrcode-generator failed to load."); | ||
| return; | ||
| } | ||
|
|
||
| // --- 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 = 2; | ||
| 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 | ||
|
|
||
| function loadQRCodeGenerator(callback) { | ||
| if (typeof window.qrcode !== "undefined") { | ||
| callback(); | ||
| return; | ||
| } | ||
|
|
||
| var script = document.createElement("script"); | ||
| script.src = "https://cdn.jsdelivr.net/npm/qrcode-generator@1.4.4/qrcode.min.js"; | ||
| script.onload = callback; | ||
| script.onerror = function () { | ||
| console.error("Failed to load qrcode-generator from CDN."); | ||
| }; | ||
| document.head.appendChild(script); | ||
| } | ||
|
|
||
| function init() { | ||
| loadQRCodeGenerator(function () { | ||
| applyQRPatch(); | ||
| }); | ||
| } | ||
|
|
||
| // Wait for all deferred scripts to finish evaluating before loading and patching. | ||
| if (document.readyState === "loading") { | ||
| document.addEventListener("DOMContentLoaded", init); | ||
| } else { | ||
| init(); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| })(); | ||
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.