From f547adf87f513c2b9bbf0a3b69ca80891bcd4e4b Mon Sep 17 00:00:00 2001 From: Dmitrii Selivanov Date: Mon, 15 Nov 2021 03:39:24 +0300 Subject: [PATCH] Initial commit --- .gitattributes | 2 + .gitignore | 1 + README.md | 1 + background.js | 46 +++++++++++++++++++ content-script.js | 0 manifest.json | 33 +++++++++++++ popup.css | 13 ++++++ popup.html | 12 +++++ popup.js | 10 ++++ script.js | 115 ++++++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 233 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 README.md create mode 100644 background.js create mode 100644 content-script.js create mode 100644 manifest.json create mode 100644 popup.css create mode 100644 popup.html create mode 100644 popup.js create mode 100644 script.js diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ffd2a4a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.idea diff --git a/README.md b/README.md new file mode 100644 index 0000000..5ffaff6 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# youtube-dislike-chrome-extension \ No newline at end of file diff --git a/background.js b/background.js new file mode 100644 index 0000000..9e4d8a3 --- /dev/null +++ b/background.js @@ -0,0 +1,46 @@ +const apiUrl = "https://return-youtube-dislike-api.azurewebsites.net"; + +chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { + if (request.message === "get_auth_token") { + // chrome.identity.getAuthToken({ interactive: true }, function (token) { + // console.log(token); + // chrome.identity.getProfileUserInfo(function (userInfo) { + // console.log(JSON.stringify(userInfo)); + // }); + // }); + } else if (request.message === "log_off") { + // console.log("logging off"); + // chrome.identity.clearAllCachedAuthTokens(() => console.log("logged off")); + } else if (request.message == "set_state") { + console.log(request); + // chrome.identity.getAuthToken({ interactive: true }, function (token) { + let token = ""; + fetch(`${apiUrl}/votes?videoId=${request.videoId}`, { + method: "GET", + headers: { + Accept: "application/json", + Authorization: "Bearer " + token, + }, + }) + .then((response) => response.json()) + .then((response) => { + console.log(response); + sendResponse(response); + }) + .catch(); + //}); + return true; + } +}); + +chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => { + if (changeInfo.status == "complete") { + if (tab.url && tab.url.indexOf("youtube.") < 0) return; + chrome.tabs.get(tabId, (tab) => { + chrome.scripting.executeScript({ + target: { tabId: tab.id }, + files: ["script.js"], + }); + }); + } +}); diff --git a/content-script.js b/content-script.js new file mode 100644 index 0000000..e69de29 diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..0a005f5 --- /dev/null +++ b/manifest.json @@ -0,0 +1,33 @@ +{ + "name": "Youtube Dislike Button", + "description": "Returns ability to see dislikes", + "version": "0.0.0.1", + "manifest_version": 3, + "background": { + "service_worker": "background.js" + }, + "permissions": [ + "activeTab", + "scripting" + ], + "host_permissions": [ + "*://*.youtube.com/*" + ], + "action": { + "default_popup": "popup.html" + }, + "oauth2": { + "client_id": "292556337651-kbmq2pduaejrol457a5s1089ut6ug0u9.apps.googleusercontent.com", + "scopes": [ + "openid" + ] + }, + "content_scripts": [ + { + "matches": ["*://*.youtube.com/*"], + "js": ["content-script.js"], + "run_at": "document_idle" + } + ], + "key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiHSM4q3Ti5gb2j6lfOwlNvrl0IGtIVOdZZNckjd15gAZh5oo9BfO1QwqJQD39KdiyELCfTduxcDeQM5RLZ6V59iK5Lt04edFS6hF7gYrB5TMQ7pgFLetT4EUgZusu9PgApU11ZWrS+VfL/2XIcIGMdpBQ1rq7X6s7ffWoQzQiQhjZUoNaCMvyI0iCEH7RS4qylV5el80kH6RyUXCDFoo1zr06BiZOj65fALgbUmiz1RstI5/bsCgA44DHXonA7d/dvc5jWvXEuZdNWvuvYdpPPxGebSEktn0+waI+VXV5vggsTfILerIsKNb6rwTlujB6Ke98cKhIF3wGqUuvwdV6wIDAQAB" +} diff --git a/popup.css b/popup.css new file mode 100644 index 0000000..1a2f9b0 --- /dev/null +++ b/popup.css @@ -0,0 +1,13 @@ +button { + height: 30px; + width: 30px; + outline: none; + margin: 10px; + border: none; + border-radius: 2px; +} + +button.current { + box-shadow: 0 0 0 2px white, + 0 0 0 4px black; +} diff --git a/popup.html b/popup.html new file mode 100644 index 0000000..0c17331 --- /dev/null +++ b/popup.html @@ -0,0 +1,12 @@ + + + + + + + + + +

Returns ability to see dislikes

+ + diff --git a/popup.js b/popup.js new file mode 100644 index 0000000..9dbdffb --- /dev/null +++ b/popup.js @@ -0,0 +1,10 @@ +/* popup-script.js */ +document.querySelector('#login') +.addEventListener('click', function () { + chrome.runtime.sendMessage({ message: 'get_auth_token' }); +}); + +document.querySelector('#log_off') +.addEventListener('click', function () { + chrome.runtime.sendMessage({ message: 'log_off' }); +}); diff --git a/script.js b/script.js new file mode 100644 index 0000000..2a2482d --- /dev/null +++ b/script.js @@ -0,0 +1,115 @@ +function getButtons() { + 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"; +} + +function setLikes(likesCount) { + getButtons().children[0].querySelector("#text").innerText = likesCount; +} + +function setDislikes(dislikesCount) { + getButtons().children[1].querySelector("#text").innerText = dislikesCount; +} + +function setState() { + chrome.runtime.sendMessage( + { + message: "set_state", + videoId: getVideoId(), + state: getState(), + }, + function (response) { + if (response != undefined) { + // setLikes(response.likes); + console.log(response); + setDislikes(response.dislikes); + } else { + } + } + ); +} + +function likeClicked() { + console.log("like" + getState()); + setState(); +} + +function dislikeClicked() { + console.log("dislike" + getState()); + setState(); +} + +function setInitalState() { + setState(); +} + +function getVideoId() { + const urlParams = new URLSearchParams(window.location.search); + const videoId = urlParams.get("v"); + return videoId; +} + +function isVideoLoaded() { + const videoId = getVideoId(); + return ( + document.querySelector(`ytd-watch-flexy[video-id='${videoId}']`) !== null + ); +} + +function setEventListeners(evt) { + function checkForJS_Finish() { + if (getButtons()?.offsetParent && isVideoLoaded()) { + clearInterval(jsInitChecktimer); + 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) { + var jsInitChecktimer = setInterval(checkForJS_Finish, 111); + } +} + +setEventListeners(); + +//window.addEventListener("hashchange", setEventListeners, false);