-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
115 lines (94 loc) · 2.89 KB
/
content.js
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
(async function() {
let rekognition;
function blurImage(img) {
img.style.filter = 'blur(100px)';
}
function removeBlur(img) {
img.style.filter = 'inherit';
}
function initializeAWS() {
AWS.config.update({
region: CONFIG.AWS_REGION,
credentials: new AWS.Credentials({
accessKeyId: CONFIG.AWS_ACCESS_KEY_ID,
secretAccessKey: CONFIG.AWS_SECRET_ACCESS_KEY
})
});
rekognition = new AWS.Rekognition();
}
async function compareFaces(sourceImage, targetImage) {
initializeAWS();
const sourceBuffer = base64ToArrayBuffer(sourceImage);
const targetBuffer = base64ToArrayBuffer(targetImage);
const params = {
SourceImage: {
Bytes: new Uint8Array(sourceBuffer)
},
TargetImage: {
Bytes: new Uint8Array(targetBuffer)
},
SimilarityThreshold: 90
};
try {
const response = await rekognition.compareFaces(params).promise();
console.log(response);
return response.FaceMatches.length > 0;
} catch (error) {
console.error('Error comparing faces:', error);
return false;
}
}
async function getBase64FromUrl(url) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch: ${response.status} ${response.statusText}`);
}
const blob = await response.blob();
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.onerror = reject;
reader.readAsDataURL(blob);
});
} catch (error) {
console.error('Error:', error);
throw error;
}
}
function base64ToArrayBuffer(base64) {
const base64String = base64.replace(/^data:image\/(png|jpeg|jpg);base64,/, '');
const binaryString = window.atob(base64String);
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes.buffer;
}
let sourceImage = await getBase64FromUrl(CONFIG.IMAGE_URL);
async function processTwitterImages() {
const twitterImages = document.querySelectorAll('img[src*="pbs.twimg.com/media"]');
for (const img of twitterImages) {
if (img.dataset.processed) continue
img.dataset.processed = 'true';
blurImage(img.parentElement);
const targetImage = await getBase64FromUrl(img.src);
compareFaces(sourceImage, targetImage)
.then(function (matched) {
console.log(matched);
if (!matched) {
removeBlur(img.parentElement);
}
})
.catch(function () {
removeBlur(img.parentElement);
})
}
}
const observer = new MutationObserver(processTwitterImages);
observer.observe(document.body, {
childList: true,
subtree: true
});
processTwitterImages();
})()