123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- (function (extensionId) {
- let storedData = {
- dislikes: 0
- };
- function cLog(message, writer) {
- message = `[return youtube dislike]: ${message}`;
- if (writer) {
- writer(message);
- } else {
- console.log(message);
- }
- }
- function getButtons() {
- //--- If Menu Element Is Displayed: ---//
- if (document.getElementById('menu-container').offsetParent === null) {
- return document.querySelector(
- "ytd-menu-renderer.ytd-watch-metadata > div"
- );
- //--- If Menu Element Isnt Displayed: ---//
- } else {
- return document
- .getElementById("menu-container")
- ?.querySelector("#top-level-buttons-computed");
- }
- }
- function getLikeButton() {
- return getButtons().children[0];
- }
- function getDislikeButton() {
- return getButtons().children[1];
- }
- function isVideoLiked() {
- return getLikeButton().classList.contains("style-default-active");
- }
- function isVideoDisliked() {
- return getDislikeButton().classList.contains("style-default-active");
- }
- function isVideoNotLiked() {
- return getLikeButton().classList.contains("style-text");
- }
- function isVideoNotDisliked() {
- return getDislikeButton().classList.contains("style-text");
- }
- function getState() {
- if (isVideoLiked()) {
- return "liked";
- }
- if (isVideoDisliked()) {
- return "disliked";
- }
- return "neutral";
- }
- //--- Sets The Likes And Dislikes Values ---//
- function setLikes(likesCount) {
- getButtons().children[0].querySelector("#text").innerText = likesCount;
- }
- function setDislikes(dislikesCount) {
- getButtons().children[1].querySelector("#text").innerText = dislikesCount;
- }
- function setState() {
- let statsSet = false;
- chrome.runtime.sendMessage(
- extensionId,
- {
- message: "fetch_from_youtube",
- videoId: getVideoId(window.location.href),
- },
- function (response) {
- if (response != undefined) {
- cLog("response from youtube:");
- cLog(JSON.stringify(response));
- try {
- if (response.likes && response.dislikes) {
- const formattedDislike = numberFormat(response.dislikes);
- setDislikes(formattedDislike);
- storedData.dislikes = parseInt(response.dislikes);
- createRateBar(response.likes, response.dislikes);
- statsSet = true;
- }
- } catch (e) {
- statsSet = false;
- }
- }
- }
- );
- chrome.runtime.sendMessage(
- extensionId,
- {
- message: "set_state",
- videoId: getVideoId(window.location.href),
- state: getState(),
- },
- function (response) {
- cLog("response from api:");
- cLog(JSON.stringify(response));
- if (response != undefined && !statsSet) {
- const formattedDislike = numberFormat(response.dislikes);
- // setLikes(response.likes);
- setDislikes(formattedDislike);
- createRateBar(response.likes, response.dislikes);
- } else {
- }
- }
- );
- }
- function likeClicked() {
- console.log("Dislike State:", getState());
- // setState();
- }
- function dislikeClicked() {
- let state = getState();
- console.log("Dislike State:",state);
- if (state == 'disliked') {
- storedData.dislikes++;
- setDislikes(numberFormat( storedData.dislikes))
- } else if (state == 'neutral') {
- storedData.dislikes--;
- setDislikes(numberFormat(storedData.dislikes))
- }
- // setState();
- }
- function setInitalState() {
- setState();
- // setTimeout(() => sendVideoIds(), 1500);
- }
- function getVideoId(url) {
- const urlObject = new URL(url);
- const videoId = urlObject.searchParams.get("v");
- return videoId;
- }
- function isVideoLoaded() {
- const videoId = getVideoId(window.location.href);
- return (
- document.querySelector(`ytd-watch-flexy[video-id='${videoId}']`) !== null
- );
- }
- function roundDown(num) {
- if (num < 1000) return num;
- const int = Math.floor(Math.log10(num) - 2);
- const decimal = int + (int % 3 ? 1 : 0);
- const value = Math.floor(num / 10 ** decimal);
- return value * (10 ** decimal);
- }
- function numberFormat(numberState) {
- const userLocales = navigator.language;
- const formatter = Intl.NumberFormat(userLocales, {
- notation: 'compact',
- minimumFractionDigits: 1,
- maximumFractionDigits: 1
- });
- return formatter.format(roundDown(numberState)).replace(/\.0|,0/, '');
- }
- var jsInitChecktimer = null;
- function setEventListeners(evt) {
- function checkForJS_Finish() {
- if (getButtons()?.offsetParent && isVideoLoaded()) {
- clearInterval(jsInitChecktimer);
- jsInitChecktimer = null;
- const buttons = getButtons();
- if (!window.returnDislikeButtonlistenersSet) {
- buttons.children[0].addEventListener("click", likeClicked);
- buttons.children[1].addEventListener("click", dislikeClicked);
- window.returnDislikeButtonlistenersSet = true;
- }
- setInitalState();
- }
- }
- if (window.location.href.indexOf("watch?") >= 0) {
- 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 +
- 8;
- const widthPercent =
- likes + dislikes > 0 ? (likes / (likes + dislikes)) * 100 : 50;
- if (!rateBar) {
- document.getElementById("menu-container").insertAdjacentHTML(
- "beforeend",
- `
- <div class="ryd-tooltip" style="width: ${widthPx}px">
- <div class="ryd-tooltip-bar-container">
- <div
- id="return-youtube-dislike-bar-container"
- style="width: 100%; height: 2px;"
- >
- <div
- id="return-youtube-dislike-bar"
- style="width: ${widthPercent}%; height: 100%"
- ></div>
- </div>
- </div>
- <tp-yt-paper-tooltip position="top" id="ryd-dislike-tooltip" class="style-scope ytd-sentiment-bar-renderer" role="tooltip" tabindex="-1">
- <!--css-build:shady-->${likes.toLocaleString()} / ${dislikes.toLocaleString()}
- </tp-yt-paper-tooltip>
- </div>
- `
- );
- } else {
- document.getElementById(
- "return-youtube-dislike-bar-container"
- ).style.width = widthPx + "px";
- document.getElementById("return-youtube-dislike-bar").style.width =
- widthPercent + "%";
- document.querySelector(
- "#ryd-dislike-tooltip > #tooltip"
- ).innerHTML = `${likes.toLocaleString()} / ${dislikes.toLocaleString()}`;
- }
- }
- // 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"));
|