Merge pull request #14 from Anarios/release/0_0_0_4

Release 0.0.0.4 for chrome
This commit is contained in:
Dmitrii Selivanov authored and GitHub committed 2021-11-19 16:59:39 +03:00
commit 2dd6b0f29b
8 files changed
+183 -72

No files matched your search

+29 -15
View File
@@ -1,6 +1,14 @@
const apiUrl = "https://return-youtube-dislike-api.azurewebsites.net"; const apiUrl = "https://return-youtube-dislike-api.azurewebsites.net";
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { // Security token causes issues if extension is reloaded/updated while several tabs are open
// 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) => {
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);
@@ -12,7 +20,7 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
// console.log("logging off"); // console.log("logging off");
// chrome.identity.clearAllCachedAuthTokens(() => console.log("logged off")); // chrome.identity.clearAllCachedAuthTokens(() => console.log("logged off"));
} else if (request.message == "set_state") { } else if (request.message == "set_state") {
console.log(request); // console.log(request);
// chrome.identity.getAuthToken({ interactive: true }, function (token) { // chrome.identity.getAuthToken({ interactive: true }, function (token) {
let token = ""; let token = "";
fetch(`${apiUrl}/votes?videoId=${request.videoId}`, { fetch(`${apiUrl}/votes?videoId=${request.videoId}`, {
@@ -24,23 +32,29 @@ chrome.runtime.onMessage.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 = [];
}
}
}
);
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => { const sentIds = new Set();
if (changeInfo.status == "complete") { let toSend = [];
if (tab.url && tab.url.indexOf("youtube.") < 0) return;
chrome.tabs.get(tabId, (tab) => {
chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ["script.js"],
});
});
}
});
Binary file not shown.
+10
View File
@@ -0,0 +1,10 @@
chrome.runtime.sendMessage({}, (securityToken) => {
var script = document.createElement("script");
script.setAttribute("security-token", securityToken);
script.setAttribute("extension-id", chrome.runtime.id);
script.src = chrome.runtime.getURL("script.js");
script.onload = function () {
this.remove();
};
(document.head || document.documentElement).appendChild(script);
});
+14
View File
@@ -0,0 +1,14 @@
#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;
}
+12 -3
View File
@@ -1,12 +1,11 @@
{ {
"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"
}, },
"permissions": ["activeTab", "scripting"],
"host_permissions": ["*://*.youtube.com/*"], "host_permissions": ["*://*.youtube.com/*"],
"action": { "action": {
"default_popup": "popup.html" "default_popup": "popup.html"
@@ -19,7 +18,17 @@
{ {
"matches": ["*://*.youtube.com/*"], "matches": ["*://*.youtube.com/*"],
"js": ["content-script.js"], "js": ["content-script.js"],
"run_at": "document_idle" "run_at": "document_start",
"css": ["content-style.css"]
}
],
"externally_connectable": {
"matches": ["*://*.youtube.com/*"]
},
"web_accessible_resources": [
{
"resources": ["script.js"],
"matches": ["*://*.youtube.com/*"]
} }
] ]
} }
+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' });
}); });
+115 -50
View File
@@ -1,34 +1,35 @@
function getButtons() { (function (extensionId) {
function getButtons() {
return document return document
.getElementById("menu-container") .getElementById("menu-container")
?.querySelector("#top-level-buttons-computed"); ?.querySelector("#top-level-buttons-computed");
} }
function getLikeButton() { function getLikeButton() {
return getButtons().children[0]; return getButtons().children[0];
} }
function getDislikeButton() { function getDislikeButton() {
return getButtons().children[1]; return getButtons().children[1];
} }
function isVideoLiked() { function isVideoLiked() {
return getLikeButton().classList.contains("style-default-active"); return getLikeButton().classList.contains("style-default-active");
} }
function isVideoDisliked() { function isVideoDisliked() {
return getDislikeButton().classList.contains("style-default-active"); return getDislikeButton().classList.contains("style-default-active");
} }
function isVideoNotLiked() { function isVideoNotLiked() {
return getLikeButton().classList.contains("style-text"); return getLikeButton().classList.contains("style-text");
} }
function isVideoNotDisliked() { function isVideoNotDisliked() {
return getDislikeButton().classList.contains("style-text"); return getDislikeButton().classList.contains("style-text");
} }
function getState() { function getState() {
if (isVideoLiked()) { if (isVideoLiked()) {
return "liked"; return "liked";
} }
@@ -36,72 +37,77 @@ function getState() {
return "disliked"; return "disliked";
} }
return "neutral"; return "neutral";
} }
function setLikes(likesCount) { function setLikes(likesCount) {
getButtons().children[0].querySelector("#text").innerText = likesCount; getButtons().children[0].querySelector("#text").innerText = likesCount;
} }
function setDislikes(dislikesCount) { function setDislikes(dislikesCount) {
getButtons().children[1].querySelector("#text").innerText = dislikesCount; getButtons().children[1].querySelector("#text").innerText = dislikesCount;
} }
function setState() { function setState() {
chrome.runtime.sendMessage( chrome.runtime.sendMessage(
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);
createRateBar(response.likes, response.dislikes);
} else { } else {
} }
} }
); );
} }
function likeClicked() { function likeClicked() {
console.log("like" + getState()); // console.log("like" + getState());
// setState();
}
function dislikeClicked() {
// console.log("dislike" + getState());
// setState();
}
function setInitalState() {
setState(); setState();
} setTimeout(() => sendVideoIds(), 1500);
}
function dislikeClicked() { function getVideoId(url) {
console.log("dislike" + getState()); const urlObject = new URL(url);
setState(); const videoId = urlObject.searchParams.get("v");
}
function setInitalState() {
setState();
}
function getVideoId() {
const urlParams = new URLSearchParams(window.location.search);
const videoId = urlParams.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
); );
} }
function numberFormat(numberState) { function numberFormat(numberState) {
const userLocales = navigator.language; const userLocales = navigator.language;
const formatter = Intl.NumberFormat(userLocales, { notation: "compact" }); const formatter = Intl.NumberFormat(userLocales, { notation: "compact" });
return formatter.format(numberState); return formatter.format(numberState);
} }
function setEventListeners(evt) { var jsInitChecktimer = null;
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 @@ function setEventListeners(evt) {
} }
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);
}
} }
}
setEventListeners(); function createRateBar(likes, dislikes) {
var rateBar = document.getElementById(
"return-youtube-dislike-bar-container"
);
const widthPx =
getButtons().children[0].clientWidth +
getButtons().children[1].clientWidth;
//window.addEventListener("hashchange", setEventListeners, false); 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();
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>