Repository navigation
Expand file tree
/
Copy pathcontent-scripts.js
More file actions
134 lines (104 loc) Β· 4.37 KB
/
Copy pathcontent-scripts.js
File metadata and controls
134 lines (104 loc) Β· 4.37 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
// More API functions here:
// https://github.com/googlecreativelab/teachablemachine-community/tree/master/libraries/image
// Link to your model provided by Teachable Machine export panel
// π₯ You can change this (You probably should too)!!! π₯
const URL = "https://teachablemachine.withgoogle.com/models/t4m8pSTmN/";
let model, webcam, labelContainer, maxPredictions;
// Load the image model and setup the webcam
async function init() {
const modelURL = URL + "model.json";
const metadataURL = URL + "metadata.json";
// Load the model and metadata
model = await tmImage.load(modelURL, metadataURL);
maxPredictions = model.getTotalClasses();
// Convenience function to setup a webcam
const flip = true;
webcam = new tmImage.Webcam(200, 200, flip);
await webcam.setup();
await webcam.play();
document.getElementById("webcam-container").appendChild(webcam.canvas);
// Loops through the predictor once every second
setInterval(loop, 1000);
}
async function loop() {
webcam.update(); // Update the webcam frame
await predict(); // Make prediction
}
// Run the webcam image through the image model
async function predict() {
// Predict takes in webcam footage
const prediction = await model.predict(webcam.canvas);
// Sort predictions by probability (descending)
prediction.sort(function (a, b) {
return b.probability - a.probability;
});
// If the most probable prediction is "Thumbs Up"
if (prediction[0].className === "Thumbs Up") {
// Get all post elements
const postElementNodeList = document.querySelectorAll("article");
const postElementArray = Array.from(postElementNodeList);
// Get the most visible post element
const mostVisiblePostElement = getElementMostVisible(postElementArray);
// Get the "content" container element from the post element
const contentContainerElement = mostVisiblePostElement.children[2];
const imageElement = contentContainerElement.querySelector("img.FFVAD"); // Get nested image element
const videoElement = contentContainerElement.querySelector("video"); // Get nested video element
const likeButtonImage = mostVisiblePostElement.querySelector(
`svg[aria-label="Like"]`
); // Get image icon
// If the post is alread liked (the like button image is gone)
if (likeButtonImage == null) {
return;
}
if (videoElement !== null) {
// Handle Videos
const likeButtonImageContainer = likeButtonImage.parentNode; // The `svg` click events are cancelled for some reason, so we click on it's parent
likeButtonImageContainer.click();
} else {
// Handle Images
doubleClick(imageElement);
}
}
}
function doubleClick(element) {
console.log("double clicked element β¬");
const clickEvent = document.createEvent("MouseEvents");
clickEvent.initEvent("dblclick", true, true);
element.dispatchEvent(clickEvent);
}
function getElementMostVisible(elements) {
// Filter out elements that don't have a top or bottom that's visible
const elementsThatAreVisible = elements.filter(function (element) {
const bounds = element.getBoundingClientRect();
return bounds.top > 0 || bounds.bottom > 0;
});
// Sort elements by their top (y) position
elementsThatAreVisible.sort(function (a, b) {
const boundsA = a.getBoundingClientRect();
const boundsB = b.getBoundingClientRect();
return boundsA.top - boundsB.top;
});
// Extract out the two elements that are visible
const [elementA, elementB] = elementsThatAreVisible;
const windowHeight = window.innerHeight;
// Determine the percentage of the screen that is occupied
// by each of the two elements
const elementAPercentage =
elementA.getBoundingClientRect().bottom / windowHeight;
const elementBPercentage =
(windowHeight - elementB.getBoundingClientRect().top) / windowHeight;
// Return the most visible element
if (elementAPercentage > elementBPercentage) {
console.log(elementA, "is the most visible element π");
return elementA;
}
console.log(elementB, "is the most visible element π");
return elementB;
}
// Adds webcam element to the document (we need it, even though we're going to hide it)
const webcamContainer = document.createElement("div");
webcamContainer.id = "webcam-container";
webcamContainer.style.display = "none"; // Don't display the webcam on the site
document.body.appendChild(webcamContainer);
// Start predicting
init();