-
-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathsocial-share-button-qr.js
More file actions
176 lines (148 loc) · 5.55 KB
/
Copy pathsocial-share-button-qr.js
File metadata and controls
176 lines (148 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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");
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;
}
// 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;
// --- 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();
}
})();