Initial commit

This commit is contained in:
Dmitrii Selivanov
2021-11-15 03:39:24 +03:00
commit f547adf87f
10 changed files with 233 additions and 0 deletions

2
.gitattributes vendored Normal file
View File

@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*.idea

1
README.md Normal file
View File

@@ -0,0 +1 @@
# youtube-dislike-chrome-extension

46
background.js Normal file
View File

@@ -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"],
});
});
}
});

0
content-script.js Normal file
View File

33
manifest.json Normal file
View File

@@ -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"
}

13
popup.css Normal file
View File

@@ -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;
}

12
popup.html Normal file
View File

@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="button.css">
</head>
<body>
<!--<button id="login">Login</button>-->
<!--<button id="log_off">Log off</button>-->
<!--<script src="popup.js"></script>-->
<p>Returns ability to see dislikes</p>
</body>
</html>

10
popup.js Normal file
View File

@@ -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' });
});

115
script.js Normal file
View File

@@ -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);