MCPcopy
hub / github.com/zxlie/FeHelper / decode

Function decode

apps/qr-code/content-script.js:3–51  ·  view source on GitHub ↗
(imgUrl)

Source from the content-addressed store, hash-verified

1window.qrcodeContentScript = function () {
2
3 let decode = function (imgUrl) {
4
5 function loadImage(src) {
6 return new Promise(resolve => {
7 let image = new Image();
8 image.setAttribute('crossOrigin', 'Anonymous');
9 image.src = src;
10 image.onload = function () {
11 let canvas;
12 try {
13 let width = this.naturalWidth;
14 let height = this.naturalHeight;
15 canvas = document.createElement('canvas');
16 canvas.style.cssText = 'position:absolute;top:-10000px;left:-10000px';
17 document.body.appendChild(canvas);
18 canvas.setAttribute('id', 'qr-canvas');
19 canvas.height = height + 100;
20 canvas.width = width + 100;
21 let context = canvas.getContext('2d');
22 context.fillStyle = 'rgb(255,255,255)';
23 context.fillRect(0, 0, canvas.width, canvas.height);
24 context.imageSmoothingEnabled = false;
25 context.drawImage(image, 0, 0, width, height, 50, 50, width, height);
26 resolve(canvas.toDataURL());
27 } catch (err) {
28 resolve(src);
29 } finally {
30 if (canvas && canvas.parentNode) {
31 canvas.parentNode.removeChild(canvas);
32 }
33 }
34 };
35 image.onerror = function () {
36 resolve(src);
37 };
38 });
39 }
40
41 loadImage(imgUrl).then(dataUrl => {
42
43 chrome.runtime.sendMessage({
44 type: 'fh-dynamic-any-thing',
45 thing: 'qr-decode',
46 params: {
47 uri: dataUrl || imgUrl
48 }
49 });
50 });
51 };
52
53 return {decode}
54};

Callers

nothing calls this directly

Calls 1

loadImageFunction · 0.85

Tested by

no test coverage detected