Firefox updated to 0.0.0.6

This commit is contained in:
Dmitrii Selivanov committed 2021-11-22 00:52:36 +03:00
1 parent 62759efb04
commit 5c85cfc425
7 files changed
+209 -73

No files matched your search

+61 -1
View File
@@ -45,14 +45,33 @@ browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
}
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) => {
let result = getDislikesFromYoutubeResponse(text);
sendResponse(result);
try {
sendUserSubmittedStatisticsToApi({
...result,
videoId: request.videoId,
});
} catch {}
});
return true;
}
});
const sentIds = new Set();
let toSend = [];
let lastCalled = new Date();
browser.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
if (changeInfo.status == "complete") {
if (changeInfo.status == "complete" && new Date() - lastCalled > 100) {
lastCalled = new Date();
console.log("Tab update complete");
if (tab.url && tab.url.indexOf("youtube.") < 0) return;
browser.tabs.get(tabId, (tab) => {
browser.tabs.executeScript(tab.id, {
@@ -61,3 +80,44 @@ browser.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
});
}
});
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),
});
}