state.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. import {
  2. getLikeButton,
  3. getDislikeButton,
  4. getButtons,
  5. getLikeTextContainer,
  6. getDislikeTextContainer,
  7. } from "./buttons";
  8. import { createRateBar } from "./bar";
  9. import {
  10. getBrowser,
  11. getVideoId,
  12. cLog,
  13. numberFormat,
  14. getColorFromTheme,
  15. } from "./utils";
  16. import { localize } from "./utils";
  17. import { createStarRating } from "./starRating";
  18. //TODO: Do not duplicate here and in ryd.background.js
  19. const apiUrl = "https://returnyoutubedislikeapi.com";
  20. const LIKED_STATE = "LIKED_STATE";
  21. const DISLIKED_STATE = "DISLIKED_STATE";
  22. const NEUTRAL_STATE = "NEUTRAL_STATE";
  23. let extConfig = {
  24. disableVoteSubmission: false,
  25. coloredThumbs: false,
  26. coloredBar: false,
  27. colorTheme: "classic",
  28. numberDisplayFormat: "compactShort",
  29. showTooltipPercentage: false,
  30. tooltipPercentageMode: "dash_like",
  31. numberDisplayReformatLikes: false,
  32. };
  33. let storedData = {
  34. likes: 0,
  35. dislikes: 0,
  36. previousState: NEUTRAL_STATE,
  37. };
  38. function isMobile() {
  39. return location.hostname == "m.youtube.com";
  40. }
  41. function isShorts() {
  42. return location.pathname.startsWith("/shorts");
  43. }
  44. function isNewDesign() {
  45. return document.getElementById("comment-teaser") !== null;
  46. }
  47. let mutationObserver = new Object();
  48. if (isShorts() && mutationObserver.exists !== true) {
  49. cLog("initializing mutation observer");
  50. mutationObserver.options = {
  51. childList: false,
  52. attributes: true,
  53. subtree: false,
  54. };
  55. mutationObserver.exists = true;
  56. mutationObserver.observer = new MutationObserver(function (
  57. mutationList,
  58. observer
  59. ) {
  60. mutationList.forEach((mutation) => {
  61. if (
  62. mutation.type === "attributes" &&
  63. mutation.target.nodeName === "TP-YT-PAPER-BUTTON" &&
  64. mutation.target.id === "button"
  65. ) {
  66. // cLog('Short thumb button status changed');
  67. if (mutation.target.getAttribute("aria-pressed") === "true") {
  68. mutation.target.style.color =
  69. mutation.target.parentElement.parentElement.id === "like-button"
  70. ? getColorFromTheme(true)
  71. : getColorFromTheme(false);
  72. } else {
  73. mutation.target.style.color = "unset";
  74. }
  75. return;
  76. }
  77. cLog(
  78. "unexpected mutation observer event: " + mutation.target + mutation.type
  79. );
  80. });
  81. });
  82. }
  83. function isLikesDisabled() {
  84. // return true if the like button's text doesn't contain any number
  85. if (isMobile()) {
  86. return /^\D*$/.test(
  87. getButtons().children[0].querySelector(".button-renderer-text").innerText
  88. );
  89. }
  90. return /^\D*$/.test(
  91. getButtons().children[0].innerText
  92. );
  93. }
  94. function isVideoLiked() {
  95. if (isMobile()) {
  96. return (
  97. getLikeButton().querySelector("button").getAttribute("aria-label") ==
  98. "true"
  99. );
  100. }
  101. return getLikeButton().classList.contains("style-default-active");
  102. }
  103. function isVideoDisliked() {
  104. if (isMobile()) {
  105. return (
  106. getDislikeButton().querySelector("button").getAttribute("aria-label") ==
  107. "true"
  108. );
  109. }
  110. return getDislikeButton().classList.contains("style-default-active");
  111. }
  112. function getState(storedData) {
  113. if (isVideoLiked()) {
  114. return { current: LIKED_STATE, previous: storedData.previousState };
  115. }
  116. if (isVideoDisliked()) {
  117. return { current: DISLIKED_STATE, previous: storedData.previousState };
  118. }
  119. return { current: NEUTRAL_STATE, previous: storedData.previousState };
  120. }
  121. //--- Sets The Likes And Dislikes Values ---//
  122. function setLikes(likesCount) {
  123. cLog(`SET likes ${likesCount}`)
  124. getLikeTextContainer().innerText = likesCount;
  125. }
  126. function setDislikes(dislikesCount) {
  127. cLog(`SET dislikes ${dislikesCount}`)
  128. getDislikeTextContainer()?.removeAttribute('is-empty');
  129. if (!isLikesDisabled()) {
  130. if (isMobile()) {
  131. getButtons().children[1].querySelector(
  132. ".button-renderer-text"
  133. ).innerText = dislikesCount;
  134. return;
  135. }
  136. getDislikeTextContainer().innerText = dislikesCount;
  137. } else {
  138. cLog("likes count disabled by creator");
  139. if (isMobile()) {
  140. getButtons().children[1].querySelector(
  141. ".button-renderer-text"
  142. ).innerText = localize("TextLikesDisabled");
  143. return;
  144. }
  145. getDislikeTextContainer().innerText = localize("TextLikesDisabled");
  146. }
  147. }
  148. function getLikeCountFromButton() {
  149. try {
  150. if (isShorts()) {
  151. //Youtube Shorts don't work with this query. It's not necessary; we can skip it and still see the results.
  152. //It should be possible to fix this function, but it's not critical to showing the dislike count.
  153. return false;
  154. }
  155. let likeButton = getLikeButton()
  156. .querySelector("yt-formatted-string#text") ??
  157. getLikeButton().querySelector("button");
  158. let likesStr = likeButton.getAttribute("aria-label")
  159. .replace(/\D/g, "");
  160. return likesStr.length > 0 ? parseInt(likesStr) : false;
  161. }
  162. catch {
  163. return false;
  164. }
  165. }
  166. function processResponse(response, storedData) {
  167. const formattedDislike = numberFormat(response.dislikes);
  168. setDislikes(formattedDislike);
  169. if (extConfig.numberDisplayReformatLikes === true) {
  170. const nativeLikes = getLikeCountFromButton();
  171. if (nativeLikes !== false) {
  172. setLikes(numberFormat(nativeLikes));
  173. }
  174. }
  175. storedData.dislikes = parseInt(response.dislikes);
  176. storedData.likes = getLikeCountFromButton() || parseInt(response.likes);
  177. createRateBar(storedData.likes, storedData.dislikes);
  178. if (extConfig.coloredThumbs === true) {
  179. if (isShorts()) {
  180. // for shorts, leave deactivated buttons in default color
  181. let shortLikeButton = getLikeButton().querySelector(
  182. "tp-yt-paper-button#button"
  183. );
  184. let shortDislikeButton = getDislikeButton().querySelector(
  185. "tp-yt-paper-button#button"
  186. );
  187. if (shortLikeButton.getAttribute("aria-pressed") === "true") {
  188. shortLikeButton.style.color = getColorFromTheme(true);
  189. }
  190. if (shortDislikeButton.getAttribute("aria-pressed") === "true") {
  191. shortDislikeButton.style.color = getColorFromTheme(false);
  192. }
  193. mutationObserver.observer.observe(
  194. shortLikeButton,
  195. mutationObserver.options
  196. );
  197. mutationObserver.observer.observe(
  198. shortDislikeButton,
  199. mutationObserver.options
  200. );
  201. } else {
  202. getLikeButton().style.color = getColorFromTheme(true);
  203. getDislikeButton().style.color = getColorFromTheme(false);
  204. }
  205. }
  206. //Temporary disabling this - it breaks all places where getButtons()[1] is used
  207. // createStarRating(response.rating, isMobile());
  208. }
  209. // Tells the user if the API is down
  210. function displayError(error) {
  211. getButtons().children[1].querySelector("#text").innerText = localize(
  212. "textTempUnavailable"
  213. );
  214. }
  215. async function setState(storedData) {
  216. storedData.previousState = isVideoDisliked()
  217. ? DISLIKED_STATE
  218. : isVideoLiked()
  219. ? LIKED_STATE
  220. : NEUTRAL_STATE;
  221. let statsSet = false;
  222. let videoId = getVideoId(window.location.href);
  223. let likeCount = getLikeCountFromButton() || null;
  224. let response = await fetch(
  225. `${apiUrl}/votes?videoId=${videoId}&likeCount=${likeCount || ""}`,
  226. {
  227. method: "GET",
  228. headers: {
  229. Accept: "application/json",
  230. },
  231. }
  232. )
  233. .then((response) => {
  234. if (!response.ok) displayError(response.error);
  235. return response;
  236. })
  237. .then((response) => response.json())
  238. .catch(displayError);
  239. cLog("response from api:");
  240. cLog(JSON.stringify(response));
  241. if (response !== undefined && !("traceId" in response) && !statsSet) {
  242. processResponse(response, storedData);
  243. }
  244. }
  245. function setInitialState() {
  246. setState(storedData);
  247. }
  248. function initExtConfig() {
  249. initializeDisableVoteSubmission();
  250. initializeColoredThumbs();
  251. initializeColoredBar();
  252. initializeColorTheme();
  253. initializeNumberDisplayFormat();
  254. initializeTooltipPercentage();
  255. initializeTooltipPercentageMode();
  256. initializeNumberDisplayReformatLikes();
  257. }
  258. function initializeDisableVoteSubmission() {
  259. getBrowser().storage.sync.get(["disableVoteSubmission"], (res) => {
  260. if (res.disableVoteSubmission === undefined) {
  261. getBrowser().storage.sync.set({ disableVoteSubmission: false });
  262. } else {
  263. extConfig.disableVoteSubmission = res.disableVoteSubmission;
  264. }
  265. });
  266. }
  267. function initializeColoredThumbs() {
  268. getBrowser().storage.sync.get(["coloredThumbs"], (res) => {
  269. if (res.coloredThumbs === undefined) {
  270. getBrowser().storage.sync.set({ coloredThumbs: false });
  271. } else {
  272. extConfig.coloredThumbs = res.coloredThumbs;
  273. }
  274. });
  275. }
  276. function initializeColoredBar() {
  277. getBrowser().storage.sync.get(["coloredBar"], (res) => {
  278. if (res.coloredBar === undefined) {
  279. getBrowser().storage.sync.set({ coloredBar: false });
  280. } else {
  281. extConfig.coloredBar = res.coloredBar;
  282. }
  283. });
  284. }
  285. function initializeColorTheme() {
  286. getBrowser().storage.sync.get(["colorTheme"], (res) => {
  287. if (res.colorTheme === undefined) {
  288. getBrowser().storage.sync.set({ colorTheme: false });
  289. } else {
  290. extConfig.colorTheme = res.colorTheme;
  291. }
  292. });
  293. }
  294. function initializeNumberDisplayFormat() {
  295. getBrowser().storage.sync.get(["numberDisplayFormat"], (res) => {
  296. if (res.numberDisplayFormat === undefined) {
  297. getBrowser().storage.sync.set({ numberDisplayFormat: "compactShort" });
  298. } else {
  299. extConfig.numberDisplayFormat = res.numberDisplayFormat;
  300. }
  301. });
  302. }
  303. function initializeTooltipPercentage() {
  304. getBrowser().storage.sync.get(["showTooltipPercentage"], (res) => {
  305. if (res.showTooltipPercentage === undefined) {
  306. getBrowser().storage.sync.set({ showTooltipPercentage: false });
  307. } else {
  308. extConfig.showTooltipPercentage = res.showTooltipPercentage;
  309. }
  310. });
  311. }
  312. function initializeTooltipPercentageMode() {
  313. getBrowser().storage.sync.get(["tooltipPercentageMode"], (res) => {
  314. if (res.tooltipPercentageMode === undefined) {
  315. getBrowser().storage.sync.set({ tooltipPercentageMode: "dash_like" });
  316. } else {
  317. extConfig.tooltipPercentageMode = res.tooltipPercentageMode;
  318. }
  319. });
  320. }
  321. function initializeNumberDisplayReformatLikes() {
  322. getBrowser().storage.sync.get(["numberDisplayReformatLikes"], (res) => {
  323. if (res.numberDisplayReformatLikes === undefined) {
  324. getBrowser().storage.sync.set({ numberDisplayReformatLikes: false });
  325. } else {
  326. extConfig.numberDisplayReformatLikes = res.numberDisplayReformatLikes;
  327. }
  328. });
  329. }
  330. export {
  331. isMobile,
  332. isShorts,
  333. isVideoDisliked,
  334. isVideoLiked,
  335. isNewDesign,
  336. getState,
  337. setState,
  338. setInitialState,
  339. setLikes,
  340. setDislikes,
  341. getLikeCountFromButton,
  342. LIKED_STATE,
  343. DISLIKED_STATE,
  344. NEUTRAL_STATE,
  345. extConfig,
  346. initExtConfig,
  347. storedData,
  348. isLikesDisabled,
  349. };