-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
34 lines (28 loc) · 1.08 KB
/
background.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
let toxicityModel, toxicityWordIndices;
chrome.runtime.onInstalled.addListener(async function() {
chrome.storage.local.set({threshold: .4, active: true});
toxicityModel = await getToxicityModel();
toxicityWordIndices = await getToxicityWordIndices();
chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
chrome.declarativeContent.onPageChanged.addRules([
{
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: {urlContains: "youtube.com"},
})
],
actions: [new chrome.declarativeContent.ShowPageAction()]
}
]);
});
});
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
const comment = request.comment;
const inputBuffer = tf.buffer([1, 60], "float32");
comment.toLowerCase().split(" ").forEach(function(word, index) {
wordIndex = toxicityWordIndices[word];
inputBuffer.set(wordIndex, 0, index);
});
let prediction = toxicityModel.predict(inputBuffer.toTensor()).arraySync()[0][0];
sendResponse({prediction: prediction});
});