Welcome to mirror list, hosted at ThFree Co, Russian Federation.

return-youtube-dislike.background.js « chrome « Extensions - github.com/Anarios/return-youtube-dislike.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e03be5b59ec892d07acf77abb6efc1d61a214eec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const apiUrl = "https://returnyoutubedislikeapi.com";

// Security token causes issues if extension is reloaded/updated while several tabs are open
// const securityToken = Math.random().toString(36).substring(2, 15);
//
// chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
//   sendResponse(securityToken);
// });

chrome.runtime.onMessageExternal.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) => {
          sendResponse(response);
        })
        .catch();
      //});
      return true;
    } else if (request.message == "send_links") {
      toSend = toSend.concat(request.videoIds.filter((x) => !sentIds.has(x)));
      if (toSend.length >= 20) {
        fetch(`${apiUrl}/votes`, {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          body: JSON.stringify(toSend),
        });
        for (const toSendUrl of toSend) {
          sentIds.add(toSendUrl);
        }
        toSend = [];
      }
    } else if (request.message == "fetch_from_youtube") {
      fetch(`https://www.youtube.com/watch?v=${request.videoId}`, {
        method: "GET",
      })
        .then((response) => response.text())
        .then((text) => {
          var result = getDislikesFromYoutubeResponse(text);
          sendUserSubmittedStatisticsToApi({
            ...result,
            videoId: request.videoId,
          });
          sendResponse(result);
        });
    }
  }
);

const sentIds = new Set();
let toSend = [];

function getDislikesFromYoutubeResponse(htmlResponse) {
  let start =
    htmlResponse.indexOf('"videoDetails":') + '"videoDetails":'.length;
  let end =
    htmlResponse.indexOf('"isLiveContent":false}', start) +
    '"isLiveContent":false}'.length;
  if (end < start) {
    end =
      htmlResponse.indexOf('"isLiveContent":true}', start) +
      '"isLiveContent":true}'.length;
  }
  let jsonStr = htmlResponse.substring(start, end);
  let jsonResult = JSON.parse(jsonStr);
  let rating = jsonResult.averageRating;

  start = htmlResponse.indexOf('"topLevelButtons":[', end);
  start =
    htmlResponse.indexOf('"accessibilityData":', start) +
    '"accessibilityData":'.length;
  end = htmlResponse.indexOf("}", start);
  let likes = +htmlResponse.substring(start, end).replace(/\D/g, "");
  let dislikes = (likes * (5 - rating)) / (rating - 1);
  let result = {
    likes,
    dislikes: Math.round(dislikes),
    rating,
    viewCount: +jsonResult.viewCount,
  };
  return result;
}

function sendUserSubmittedStatisticsToApi(statistics) {
  fetch(`${apiUrl}/votes/user-submitted`, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify(statistics),
  });
}