Selaa lähdekoodia

Initial commit

Dmitrii Selivanov 3 vuotta sitten
sitoutus
f547adf87f
10 muutettua tiedostoa jossa 233 lisäystä ja 0 poistoa
  1. 2 0
      .gitattributes
  2. 1 0
      .gitignore
  3. 1 0
      README.md
  4. 46 0
      background.js
  5. 0 0
      content-script.js
  6. 33 0
      manifest.json
  7. 13 0
      popup.css
  8. 12 0
      popup.html
  9. 10 0
      popup.js
  10. 115 0
      script.js

+ 2 - 0
.gitattributes

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

+ 1 - 0
.gitignore

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

+ 1 - 0
README.md

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

+ 46 - 0
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"],
+      });
+    });
+  }
+});

+ 0 - 0
content-script.js


+ 33 - 0
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"
+}

+ 13 - 0
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;
+}

+ 12 - 0
popup.html

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

+ 115 - 0
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);