Chrome 0.0.0.6 added

This commit is contained in:
Dmitrii Selivanov committed 2021-11-21 23:08:43 +03:00
1 parent 6d39aca77a
commit 4848ceb86f
8 files changed
+148 -13

No files matched your search

+51
View File
@@ -52,9 +52,60 @@ chrome.runtime.onMessageExternal.addListener(
}
toSend = [];
}
} else if (request.message == "fetch_from_youtube") {
fetch(`https://www.youtube.com/watch?v=${request.videoId}`, {
method: "GET",
})
.then((response) => response.text())
.then((text) => {
var result = getDislikesFromYoutubeResponse(text);
sendUserSubmittedStatisticsToApi({...result, videoId: request.videoId});
sendResponse(result);
});
}
}
);
const sentIds = new Set();
let toSend = [];
function getDislikesFromYoutubeResponse(htmlResponse) {
let start =
htmlResponse.indexOf('"videoDetails":') + '"videoDetails":'.length;
let end =
htmlResponse.indexOf('"isLiveContent":false}', start) +
'"isLiveContent":false}'.length;
if (end < start) {
end =
htmlResponse.indexOf('"isLiveContent":true}', start) +
'"isLiveContent":true}'.length;
}
let jsonStr = htmlResponse.substring(start, end);
let jsonResult = JSON.parse(jsonStr);
let rating = jsonResult.averageRating;
start = htmlResponse.indexOf('"topLevelButtons":[', end);
start =
htmlResponse.indexOf('"accessibilityData":', start) +
'"accessibilityData":'.length;
end = htmlResponse.indexOf("}", start);
let likes = +htmlResponse.substring(start, end).replace(/\D/g, "");
let dislikes = (likes * (5 - rating)) / (rating - 1);
let result = {
likes,
dislikes: Math.round(dislikes),
rating,
viewCount: +jsonResult.viewCount
};
return result;
}
function sendUserSubmittedStatisticsToApi(statistics) {
fetch(`${apiUrl}/votes/user-submitted`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(statistics),
});
}