diff --git a/Extensions/chrome/manifest.json b/Extensions/chrome/manifest.json index 1826122..af49f2d 100644 --- a/Extensions/chrome/manifest.json +++ b/Extensions/chrome/manifest.json @@ -1,7 +1,7 @@ { "name": "Youtube Dislike Button", "description": "Returns ability to see dislikes", - "version": "0.0.0.4", + "version": "0.0.0.5", "manifest_version": 3, "background": { "service_worker": "background.js" diff --git a/Extensions/chrome/script.js b/Extensions/chrome/script.js index 9aadd3f..5adecc9 100644 --- a/Extensions/chrome/script.js +++ b/Extensions/chrome/script.js @@ -79,7 +79,7 @@ function setInitalState() { setState(); - setTimeout(() => sendVideoIds(), 1500); + // setTimeout(() => sendVideoIds(), 1500); } function getVideoId(url) { @@ -152,24 +152,24 @@ } } - 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, - }); - } + // 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(); @@ -179,9 +179,9 @@ setEventListeners(); }); - window.onscrollend = () => { - sendVideoIds(); - }; + // window.onscrollend = () => { + // sendVideoIds(); + // }; - setTimeout(() => sendVideoIds(), 1500); + // setTimeout(() => sendVideoIds(), 1500); })(document.currentScript.getAttribute("extension-id")); diff --git a/Extensions/firefox/background.js b/Extensions/firefox/background.js index 601f1ea..18a3b3c 100644 --- a/Extensions/firefox/background.js +++ b/Extensions/firefox/background.js @@ -30,14 +30,32 @@ browser.runtime.onMessage.addListener((request, sender, sendResponse) => { .catch(); //}); 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 = []; + browser.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => { if (changeInfo.status == "complete") { if (tab.url && tab.url.indexOf("youtube.") < 0) return; browser.tabs.get(tabId, (tab) => { - browser.tabs.executeScript(tab.id,{ + browser.tabs.executeScript(tab.id, { file: "script.js", }); }); diff --git a/Extensions/firefox/content-style.css b/Extensions/firefox/content-style.css new file mode 100644 index 0000000..a830e3e --- /dev/null +++ b/Extensions/firefox/content-style.css @@ -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; +} diff --git a/Extensions/firefox/manifest.json b/Extensions/firefox/manifest.json index ee386ac..588be4e 100644 --- a/Extensions/firefox/manifest.json +++ b/Extensions/firefox/manifest.json @@ -1,7 +1,7 @@ { "name": "Youtube Dislike Button", "description": "Returns ability to see dislikes", - "version": "0.0.0.2", + "version": "0.0.0.5", "manifest_version": 2, "background": { "scripts": ["background.js"] @@ -14,7 +14,8 @@ { "matches": ["*://*.youtube.com/*"], "js": ["content-script.js"], - "run_at": "document_idle" + "run_at": "document_idle", + "css": ["content-style.css"] } ] diff --git a/Extensions/firefox/script.js b/Extensions/firefox/script.js index 91d006f..04b9c71 100644 --- a/Extensions/firefox/script.js +++ b/Extensions/firefox/script.js @@ -50,7 +50,7 @@ function setState() { browser.runtime.sendMessage( { message: "set_state", - videoId: getVideoId(), + videoId: getVideoId(window.location.href), state: getState(), }, function (response) { @@ -59,6 +59,7 @@ function setState() { // setLikes(response.likes); console.log(response); setDislikes(formattedDislike); + createRateBar(response.likes, response.dislikes); } else { } } @@ -66,27 +67,28 @@ function setState() { } function likeClicked() { - console.log("like" + getState()); - setState(); + // console.log("like" + getState()); + // setState(); } function dislikeClicked() { - console.log("dislike" + getState()); - setState(); + // console.log("dislike" + getState()); + // setState(); } function setInitalState() { setState(); + // setTimeout(() => sendVideoIds(), 1500); } -function getVideoId() { - const urlParams = new URLSearchParams(window.location.search); - const videoId = urlParams.get("v"); +function getVideoId(url) { + const urlObject = new URL(url); + const videoId = urlObject.searchParams.get("v"); return videoId; } function isVideoLoaded() { - const videoId = getVideoId(); + const videoId = getVideoId(window.location.href); return ( document.querySelector(`ytd-watch-flexy[video-id='${videoId}']`) !== null ); @@ -106,6 +108,20 @@ function setEventListeners(evt) { if (!window.returnDislikeButtonlistenersSet) { buttons.children[0].addEventListener("click", likeClicked); buttons.children[1].addEventListener("click", dislikeClicked); + let lastKnownScrollPosition = 0; + let ticking = false; + // document.addEventListener('scroll', function(e) { + // lastKnownScrollPosition = window.scrollY; + // + // if (!ticking) { + // window.requestAnimationFrame(function() { + // // sendVideoIds(); + // ticking = false; + // }); + // + // ticking = true; + // } + // }); window.returnDislikeButtonlistenersSet = true; } setInitalState(); @@ -117,6 +133,69 @@ function setEventListeners(evt) { } } +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", + `
+
+
` + ); + } 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)); +// browser.runtime.sendMessage({ +// 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); + //window.addEventListener("hashchange", setEventListeners, false); diff --git a/StaticSite/files/chrome/return_youtube_dislike_LOAD_UNPACKED_0.0.0.5.zip b/StaticSite/files/chrome/return_youtube_dislike_LOAD_UNPACKED_0.0.0.5.zip new file mode 100644 index 0000000..62d37bd Binary files /dev/null and b/StaticSite/files/chrome/return_youtube_dislike_LOAD_UNPACKED_0.0.0.5.zip differ diff --git a/StaticSite/images/DislikeScreenshot.JPG b/StaticSite/images/DislikeScreenshot.JPG index 5339c5e..e9bfb09 100644 Binary files a/StaticSite/images/DislikeScreenshot.JPG and b/StaticSite/images/DislikeScreenshot.JPG differ diff --git a/StaticSite/images/ratio.jpg b/StaticSite/images/ratio.jpg new file mode 100644 index 0000000..6785997 Binary files /dev/null and b/StaticSite/images/ratio.jpg differ diff --git a/StaticSite/index.html b/StaticSite/index.html index 37cfd6c..c4bea06 100644 --- a/StaticSite/index.html +++ b/StaticSite/index.html @@ -17,10 +17,10 @@

Chrome/Firefox/Usersript extension to return youtube dislike count

-

Now available in - firefox addon store!

+ -

Version 0.0.0.4 released - ratio (like/dislike) bar added

+

Version 0.0.0.5 released - ratio (like/dislike) bar added

+

This is a Chrome/Firefox/Userscript extension that return dislike statistics to YouTube. For now, it only works if a video had public display of dislikes enabled before YouTube removed dislike stats.

@@ -44,12 +44,15 @@

Downloads:

This is an ALHPA version! It may be slow. It may be buggy.

-

Only available for chrome and Firefox now, but coming to other platforms soon

+

Only available for chrome and Firefox now and as a Userscript now, but coming to other platforms soon

Chrome:

v_0.0.0.1

v_0.0.0.2 - Better number formatting added,

v_0.0.0.3 +

0.0.0.4 removed (accidental self-ddos)

+

0.0.0.5 + - ratio bar added. Dislikes from every video on page are saved now Previous versions are disabled automatically by chrome, because Chrome doesn't like .crx extensions. @@ -60,6 +63,7 @@ Install from extension's firefox addon store page. Check for updates often - this extension is being actively improved + Latest version coming out as soon as update is approved by Mozilla