Skip to content

Commit 0c0f1ff

Browse files
authored
fix qr error on firefox (#97)
1 parent ceac490 commit 0c0f1ff

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

src/background.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,18 @@ function getQr(
3333
const qr = new Image();
3434
qr.src = dataUrl;
3535
qr.onload = () => {
36+
const devicePixelRatio = qr.width / windowWidth;
3637
const captureCanvas = document.createElement('canvas');
37-
captureCanvas.width = width;
38-
captureCanvas.height = height;
38+
captureCanvas.width = width * devicePixelRatio;
39+
captureCanvas.height = height * devicePixelRatio;
3940
const ctx = captureCanvas.getContext('2d');
4041
if (!ctx) {
4142
return;
4243
}
43-
ctx.drawImage(qr, left, top, width, height, 0, 0, width, height);
44+
ctx.drawImage(
45+
qr, left * devicePixelRatio, top * devicePixelRatio,
46+
width * devicePixelRatio, height * devicePixelRatio, 0, 0,
47+
width * devicePixelRatio, height * devicePixelRatio);
4448
const url = captureCanvas.toDataURL();
4549
qrcode.callback = (text) => {
4650
getTotp(text, passphrase);

src/content.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,14 @@ function grayLayoutUp(event: MouseEvent, passphrase: string) {
114114
return;
115115
}
116116

117-
let captureBoxLeft =
117+
const captureBoxLeft =
118118
Math.min(sessionStorage.captureBoxPositionLeft, event.clientX) + 1;
119-
let captureBoxTop =
119+
const captureBoxTop =
120120
Math.min(sessionStorage.captureBoxPositionTop, event.clientY) + 1;
121-
let captureBoxWidth =
121+
const captureBoxWidth =
122122
Math.abs(sessionStorage.captureBoxPositionLeft - event.clientX) - 1;
123-
let captureBoxHeight =
123+
const captureBoxHeight =
124124
Math.abs(sessionStorage.captureBoxPositionTop - event.clientY) - 1;
125-
captureBoxLeft *= window.devicePixelRatio;
126-
captureBoxTop *= window.devicePixelRatio;
127-
captureBoxWidth *= window.devicePixelRatio;
128-
captureBoxHeight *= window.devicePixelRatio;
129125

130126
// make sure captureBox and grayLayout is hidden
131127
setTimeout(() => {

src/qrdebug.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@ function getQrDebug(
1616
const qr = new Image();
1717
qr.src = dataUrl;
1818
qr.onload = () => {
19+
const devicePixelRatio = qr.width / windowWidth;
1920
const captureCanvas = document.createElement('canvas');
20-
captureCanvas.width = width;
21-
captureCanvas.height = height;
21+
captureCanvas.width = width * devicePixelRatio;
22+
captureCanvas.height = height * devicePixelRatio;
2223
const ctx = captureCanvas.getContext('2d');
2324
if (!ctx) {
2425
return;
2526
}
26-
ctx.drawImage(qr, left, top, width, height, 0, 0, width, height);
27+
ctx.drawImage(
28+
qr, left * devicePixelRatio, top * devicePixelRatio,
29+
width * devicePixelRatio, height * devicePixelRatio, 0, 0,
30+
width * devicePixelRatio, height * devicePixelRatio);
2731
const url = captureCanvas.toDataURL();
2832
const infoDom = document.getElementById('info');
2933
if (infoDom) {
@@ -38,7 +42,8 @@ function getQrDebug(
3842
`Screen Height: ${window.screen.height}<br>` +
3943
`Capture Width: ${qr.width}<br>` +
4044
`Capture Height: ${qr.height}<br>` +
41-
`Device Pixel Ratio:${window.devicePixelRatio}<br>` +
45+
`Device Pixel Ratio: ${devicePixelRatio} / ${
46+
window.devicePixelRatio}<br>` +
4247
`Tab ID: ${tab.id}<br>` +
4348
'<br>' +
4449
'<b>Captured Screenshot:</b>';

0 commit comments

Comments
 (0)