Release 0.0.0.4

Rate bar added
Bug caused by security token resolved by removing security token
Various bugs and console errors fixed
More like/dislike data is now saved on scroll
This commit is contained in:
Dmitrii Selivanov committed 2021-11-19 16:56:21 +03:00
1 parent 545bd2d3eb
commit 9df69d65dd
7 files changed
+132 -31

No files matched your search

+29 -9
View File
@@ -1,12 +1,14 @@
const apiUrl = "https://return-youtube-dislike-api.azurewebsites.net"; const apiUrl = "https://return-youtube-dislike-api.azurewebsites.net";
const securityToken = Math.random().toString(36).substring(2, 15);
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { // Security token causes issues if extension is reloaded/updated while several tabs are open
sendResponse(securityToken); // const securityToken = Math.random().toString(36).substring(2, 15);
}); //
// chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
// sendResponse(securityToken);
// });
chrome.runtime.onMessageExternal.addListener((request, sender, sendResponse) => { chrome.runtime.onMessageExternal.addListener(
if (request.securityToken === securityToken) { (request, sender, sendResponse) => {
if (request.message === "get_auth_token") { if (request.message === "get_auth_token") {
// chrome.identity.getAuthToken({ interactive: true }, function (token) { // chrome.identity.getAuthToken({ interactive: true }, function (token) {
// console.log(token); // console.log(token);
@@ -30,12 +32,30 @@ chrome.runtime.onMessageExternal.addListener((request, sender, sendResponse) =>
}) })
.then((response) => response.json()) .then((response) => response.json())
.then((response) => { .then((response) => {
console.log(response);
sendResponse(response); sendResponse(response);
}) })
.catch(); .catch();
//}); //});
return true; return true;
} } else if (request.message == "send_links") {
} toSend = toSend.concat(request.videoIds.filter((x) => !sentIds.has(x)));
if (toSend.length >= 20) {
fetch(`${apiUrl}/votes`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(toSend),
}); });
for (const toSendUrl of toSend) {
sentIds.add(toSendUrl);
}
toSend = [];
}
}
}
);
const sentIds = new Set();
let toSend = [];
Binary file not shown.
+16
View File
@@ -0,0 +1,16 @@
#return-youtube-dislike-bar-container {
background: #c6c6c6;
}
#return-youtube-dislike-bar {
background: #5094c8;
}
[dark] #return-youtube-dislike-bar-container {
}
[dark] #return-youtube-dislike-bar {
background: #3ea6ff;
}
+3 -2
View File
@@ -1,7 +1,7 @@
{ {
"name": "Youtube Dislike Button", "name": "Youtube Dislike Button",
"description": "Returns ability to see dislikes", "description": "Returns ability to see dislikes",
"version": "0.0.0.3", "version": "0.0.0.4",
"manifest_version": 3, "manifest_version": 3,
"background": { "background": {
"service_worker": "background.js" "service_worker": "background.js"
@@ -18,7 +18,8 @@
{ {
"matches": ["*://*.youtube.com/*"], "matches": ["*://*.youtube.com/*"],
"js": ["content-script.js"], "js": ["content-script.js"],
"run_at": "document_start" "run_at": "document_start",
"css": ["content-style.css"]
} }
], ],
"externally_connectable": { "externally_connectable": {
+2 -3
View File
@@ -4,7 +4,6 @@ document.querySelector('#login')
chrome.runtime.sendMessage({ message: 'get_auth_token' }); chrome.runtime.sendMessage({ message: 'get_auth_token' });
}); });
document.querySelector('#log_off') document.querySelector("#log_off").addEventListener("click", function () {
.addEventListener('click', function () { chrome.runtime.sendMessage({ message: "log_off" });
chrome.runtime.sendMessage({ message: 'log_off' });
}); });
+81 -16
View File
@@ -1,4 +1,4 @@
(function(securityToken, extensionId) { (function (extensionId) {
function getButtons() { function getButtons() {
return document return document
.getElementById("menu-container") .getElementById("menu-container")
@@ -48,45 +48,48 @@
} }
function setState() { function setState() {
chrome.runtime.sendMessage(extensionId, { chrome.runtime.sendMessage(
securityToken: securityToken, extensionId,
{
message: "set_state", message: "set_state",
videoId: getVideoId(), videoId: getVideoId(window.location.href),
state: getState(), state: getState(),
}, },
function (response) { function (response) {
if (response != undefined) { if (response != undefined) {
const formattedDislike = numberFormat(response.dislikes); const formattedDislike = numberFormat(response.dislikes);
// setLikes(response.likes); // setLikes(response.likes);
console.log(response);
setDislikes(formattedDislike); setDislikes(formattedDislike);
} else {} createRateBar(response.likes, response.dislikes);
} else {
}
} }
); );
} }
function likeClicked() { function likeClicked() {
console.log("like" + getState()); // console.log("like" + getState());
setState(); // setState();
} }
function dislikeClicked() { function dislikeClicked() {
console.log("dislike" + getState()); // console.log("dislike" + getState());
setState(); // setState();
} }
function setInitalState() { function setInitalState() {
setState(); setState();
setTimeout(() => sendVideoIds(), 1500);
} }
function getVideoId() { function getVideoId(url) {
const urlParams = new URLSearchParams(window.location.search); const urlObject = new URL(url);
const videoId = urlParams.get("v"); const videoId = urlObject.searchParams.get("v");
return videoId; return videoId;
} }
function isVideoLoaded() { function isVideoLoaded() {
const videoId = getVideoId(); const videoId = getVideoId(window.location.href);
return ( return (
document.querySelector(`ytd-watch-flexy[video-id='${videoId}']`) !== null document.querySelector(`ytd-watch-flexy[video-id='${videoId}']`) !== null
); );
@@ -98,10 +101,13 @@
return formatter.format(numberState); return formatter.format(numberState);
} }
var jsInitChecktimer = null;
function setEventListeners(evt) { function setEventListeners(evt) {
function checkForJS_Finish() { function checkForJS_Finish() {
if (getButtons()?.offsetParent && isVideoLoaded()) { if (getButtons()?.offsetParent && isVideoLoaded()) {
clearInterval(jsInitChecktimer); clearInterval(jsInitChecktimer);
jsInitChecktimer = null;
const buttons = getButtons(); const buttons = getButtons();
if (!window.returnDislikeButtonlistenersSet) { if (!window.returnDislikeButtonlistenersSet) {
buttons.children[0].addEventListener("click", likeClicked); buttons.children[0].addEventListener("click", likeClicked);
@@ -113,10 +119,69 @@
} }
if (window.location.href.indexOf("watch?") >= 0) { if (window.location.href.indexOf("watch?") >= 0) {
var jsInitChecktimer = setInterval(checkForJS_Finish, 111); jsInitChecktimer = setInterval(checkForJS_Finish, 111);
} }
} }
function createRateBar(likes, dislikes) {
var rateBar = document.getElementById(
"return-youtube-dislike-bar-container"
);
const widthPx =
getButtons().children[0].clientWidth +
getButtons().children[1].clientWidth;
const widthPercent =
likes + dislikes > 0 ? (likes / (likes + dislikes)) * 100 : 50;
if (!rateBar) {
document.getElementById("menu-container").insertAdjacentHTML(
"beforeend",
`<div id="return-youtube-dislike-bar-container"
style="width: ${widthPx}px;
height: 3px; margin-left: 6px;">
<div id="return-youtube-dislike-bar" style="width: ${widthPercent}%; height: 100%" ></div>
</div>`
);
} else {
document.getElementById(
"return-youtube-dislike-bar-container"
).style.width = widthPx + "px";
document.getElementById("return-youtube-dislike-bar").style.width =
widthPercent + "%";
}
}
function sendVideoIds() {
const ids = Array.from(
document.getElementsByClassName(
"yt-simple-endpoint ytd-compact-video-renderer"
)
)
.concat(
Array.from(
document.getElementsByClassName("yt-simple-endpoint ytd-thumbnail")
)
)
.filter((x) => x.href && x.href.indexOf("/watch?v=") > 0)
.map((x) => getVideoId(x.href));
chrome.runtime.sendMessage(extensionId, {
message: "send_links",
videoIds: ids,
});
}
setEventListeners(); setEventListeners();
})(document.currentScript.getAttribute("security-token"), document.currentScript.getAttribute("extension-id")); document.addEventListener("yt-navigate-finish", function (event) {
if (jsInitChecktimer !== null) clearInterval(jsInitChecktimer);
window.returnDislikeButtonlistenersSet = false;
setEventListeners();
});
window.onscrollend = () => {
sendVideoIds();
};
setTimeout(() => sendVideoIds(), 1500);
})(document.currentScript.getAttribute("extension-id"));
+1 -1
View File
@@ -20,7 +20,7 @@
<p><strong>Now <a href="https://addons.mozilla.org/en-US/firefox/addon/return-youtube-dislikes/">available</a> in <p><strong>Now <a href="https://addons.mozilla.org/en-US/firefox/addon/return-youtube-dislikes/">available</a> in
firefox addon store! </strong></p> firefox addon store! </strong></p>
<p><strong>Chrome users - update to 0.0.0.3 (via Load Unpacked, instructions below) to prevent Chrome disabling the extension</strong></p> <p><strong>Version 0.0.0.4 released - ratio (like/dislike) bar added</strong></p>
<p>This is a Chrome/Firefox/Userscript extension that return dislike statistics to YouTube. For now, it only works <p>This is a Chrome/Firefox/Userscript extension that return dislike statistics to YouTube. For now, it only works
if if
a video had public display of dislikes enabled before YouTube removed dislike stats.</p> a video had public display of dislikes enabled before YouTube removed dislike stats.</p>