From ac8d83e41fcdda5ddba5bd658913bc977c43fd3d Mon Sep 17 00:00:00 2001 From: Dmitrii Selivanov Date: Tue, 16 Nov 2021 02:22:27 +0300 Subject: [PATCH] Firefox extension added --- Extensions/chrome/manifest.json | 18 ++-- Extensions/chrome/script.js | 4 +- Extensions/firefox/background.js | 45 ++++++++++ Extensions/firefox/content-script.js | 0 Extensions/firefox/manifest.json | 27 ++++++ Extensions/firefox/packed.zip | Bin 0 -> 3057 bytes Extensions/firefox/popup.css | 13 +++ Extensions/firefox/popup.html | 12 +++ Extensions/firefox/popup.js | 10 +++ Extensions/firefox/script.js | 122 +++++++++++++++++++++++++++ 10 files changed, 236 insertions(+), 15 deletions(-) create mode 100644 Extensions/firefox/background.js create mode 100644 Extensions/firefox/content-script.js create mode 100644 Extensions/firefox/manifest.json create mode 100644 Extensions/firefox/packed.zip create mode 100644 Extensions/firefox/popup.css create mode 100644 Extensions/firefox/popup.html create mode 100644 Extensions/firefox/popup.js create mode 100644 Extensions/firefox/script.js diff --git a/Extensions/chrome/manifest.json b/Extensions/chrome/manifest.json index 0a005f5..9ce385c 100644 --- a/Extensions/chrome/manifest.json +++ b/Extensions/chrome/manifest.json @@ -1,26 +1,19 @@ { "name": "Youtube Dislike Button", "description": "Returns ability to see dislikes", - "version": "0.0.0.1", + "version": "0.0.0.2", "manifest_version": 3, "background": { "service_worker": "background.js" }, - "permissions": [ - "activeTab", - "scripting" - ], - "host_permissions": [ - "*://*.youtube.com/*" - ], + "permissions": ["activeTab", "scripting"], + "host_permissions": ["*://*.youtube.com/*"], "action": { "default_popup": "popup.html" }, "oauth2": { "client_id": "292556337651-kbmq2pduaejrol457a5s1089ut6ug0u9.apps.googleusercontent.com", - "scopes": [ - "openid" - ] + "scopes": ["openid"] }, "content_scripts": [ { @@ -28,6 +21,5 @@ "js": ["content-script.js"], "run_at": "document_idle" } - ], - "key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiHSM4q3Ti5gb2j6lfOwlNvrl0IGtIVOdZZNckjd15gAZh5oo9BfO1QwqJQD39KdiyELCfTduxcDeQM5RLZ6V59iK5Lt04edFS6hF7gYrB5TMQ7pgFLetT4EUgZusu9PgApU11ZWrS+VfL/2XIcIGMdpBQ1rq7X6s7ffWoQzQiQhjZUoNaCMvyI0iCEH7RS4qylV5el80kH6RyUXCDFoo1zr06BiZOj65fALgbUmiz1RstI5/bsCgA44DHXonA7d/dvc5jWvXEuZdNWvuvYdpPPxGebSEktn0+waI+VXV5vggsTfILerIsKNb6rwTlujB6Ke98cKhIF3wGqUuvwdV6wIDAQAB" + ] } diff --git a/Extensions/chrome/script.js b/Extensions/chrome/script.js index 41162d9..a24f1b7 100644 --- a/Extensions/chrome/script.js +++ b/Extensions/chrome/script.js @@ -58,7 +58,7 @@ function setState() { const formattedDislike = numberFormat(response.dislikes); // setLikes(response.likes); console.log(response); - setDislikes(response.dislikes); + setDislikes(formattedDislike); } else { } } @@ -94,7 +94,7 @@ function isVideoLoaded() { function numberFormat(numberState) { const userLocales = navigator.language; - const formatter = Intl.NumberFormat(userLocales, { notation: 'compact' }); + const formatter = Intl.NumberFormat(userLocales, { notation: "compact" }); return formatter.format(numberState); } diff --git a/Extensions/firefox/background.js b/Extensions/firefox/background.js new file mode 100644 index 0000000..6bc91c7 --- /dev/null +++ b/Extensions/firefox/background.js @@ -0,0 +1,45 @@ +const apiUrl = "https://localhost:7258"; + +browser.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; + } +}); + +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,{ + file: "script.js", + }); + }); + } +}); diff --git a/Extensions/firefox/content-script.js b/Extensions/firefox/content-script.js new file mode 100644 index 0000000..e69de29 diff --git a/Extensions/firefox/manifest.json b/Extensions/firefox/manifest.json new file mode 100644 index 0000000..2740ce5 --- /dev/null +++ b/Extensions/firefox/manifest.json @@ -0,0 +1,27 @@ +{ + "name": "Youtube Dislike Button", + "description": "Returns ability to see dislikes", + "version": "0.0.0.2", + "manifest_version": 2, + "background": { + "scripts": ["background.js"] + }, + "permissions": ["activeTab", "*://*.youtube.com/*"], + "browser_action": { + "default_popup": "popup.html" + }, + "content_scripts": [ + { + "matches": ["*://*.youtube.com/*"], + "js": ["content-script.js"], + "run_at": "document_idle" + } + ], + "browser_specific_settings": { + "gecko": { + "id": "returnyoutubedislike@returnyoutubedislike.com", + "update_url": "https://returnyoutubedislike.com/files/ff/updates.json" + } + } + +} diff --git a/Extensions/firefox/packed.zip b/Extensions/firefox/packed.zip new file mode 100644 index 0000000000000000000000000000000000000000..d20ec025d5dbf283984212a06814d6e6b0ba453a GIT binary patch literal 3057 zcmZ`*2UHW=7M(~ZfHV~;LIi0N2rWVYrFWEG1r(&0P(l$1f*?T*Ep#wcks5k=B2h!H zq99Fp2&iBIQKUZF58wag>+;`Ociov;cdv8Kojqr7Lp=&gb^riqft&tF6Q*vcoZm43 zSfBy`M)F&S>y92Se#ijt8{+Q%#ijZKc5VFGzQLZTjukpt_E{nw~ebW?Xa!@eZZyIc-z%6Ett zs@=$X(#}RrzilcP_N;L{+K%JFHF>YJaK1nYm0!Ck z5j5vnDBZen@qSi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tnp|3fr?@vwg2o*^U06$x%p&k|a5ddh&S29@?bPuHt_!n{i2BrW2 literal 0 HcmV?d00001 diff --git a/Extensions/firefox/popup.css b/Extensions/firefox/popup.css new file mode 100644 index 0000000..1a2f9b0 --- /dev/null +++ b/Extensions/firefox/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/Extensions/firefox/popup.html b/Extensions/firefox/popup.html new file mode 100644 index 0000000..0c17331 --- /dev/null +++ b/Extensions/firefox/popup.html @@ -0,0 +1,12 @@ + + + + + + + + + +

Returns ability to see dislikes

+ + diff --git a/Extensions/firefox/popup.js b/Extensions/firefox/popup.js new file mode 100644 index 0000000..9dbdffb --- /dev/null +++ b/Extensions/firefox/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/Extensions/firefox/script.js b/Extensions/firefox/script.js new file mode 100644 index 0000000..91d006f --- /dev/null +++ b/Extensions/firefox/script.js @@ -0,0 +1,122 @@ +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() { + browser.runtime.sendMessage( + { + message: "set_state", + videoId: getVideoId(), + state: getState(), + }, + function (response) { + if (response != undefined) { + const formattedDislike = numberFormat(response.dislikes); + // setLikes(response.likes); + console.log(response); + setDislikes(formattedDislike); + } 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 numberFormat(numberState) { + const userLocales = navigator.language; + const formatter = Intl.NumberFormat(userLocales, { notation: "compact" }); + return formatter.format(numberState); +} + +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);