@@ -11,42 +11,99 @@ export default {
1111 whiteList : [ "*://*.youtube.com/*" ] ,
1212
1313 pageScript : {
14- onClick : async function ( ) {
15- // Source code extracted from https://chrome.google.com/webstore/detail/return-youtube-dislike/gebbhagfogifgggkldgodflihgfeippi
14+ onDocumentStart : ( ) => {
15+ // youtube watch
16+ document . querySelector ( "dislike-button-view-model" ) ;
1617
17- function getVideoId ( url ) {
18- const urlObject = new URL ( url ) ;
19- const pathname = urlObject . pathname ;
20- if ( pathname . startsWith ( "/clip" ) ) {
21- return document . querySelector ( "meta[itemprop='videoId']" ) . content ;
22- } else {
23- if ( pathname . startsWith ( "/shorts" ) ) {
24- return pathname . slice ( 8 ) ;
25- }
26- return urlObject . searchParams . get ( "v" ) ;
27- }
28- }
18+ // youtube shorts
19+ document . querySelector ( "ytd-toggle-button-renderer#dislike-button" ) ;
20+ } ,
2921
30- const apiUrl = "https://returnyoutubedislikeapi.com" ;
22+ onClick : async function ( ) {
3123 let videoId = getVideoId ( location . href ) ;
32- let response = await fetch (
33- `${ apiUrl } /votes?videoId=${ videoId } &likeCount=` ,
34- {
35- method : "GET" ,
36- headers : {
37- Accept : "application/json" ,
38- } ,
39- }
40- )
41- . then ( ( response ) => {
42- if ( ! response . ok ) alert ( "Error: " + response . error ) ;
43- return response ;
44- } )
45- . then ( ( response ) => response . json ( ) )
46- . catch ( ( e ) => alert ( "ERROR: " + e ) ) ;
47-
48- console . log ( response ) ;
49- alert ( "Youtube Dislikes:\n" + JSON . stringify ( response , null , 4 ) ) ;
24+ getDislikeData ( videoId ) . then ( ( response ) => {
25+ console . log ( response ) ;
26+ alert ( "Youtube Dislikes:\n" + JSON . stringify ( response , null , 4 ) ) ;
27+ } ) ;
5028 } ,
5129 } ,
5230} ;
31+
32+ // Source code extracted from https://chrome.google.com/webstore/detail/return-youtube-dislike/gebbhagfogifgggkldgodflihgfeippi
33+ async function getDislikeData ( videoId ) {
34+ const apiUrl = "https://returnyoutubedislikeapi.com" ;
35+ try {
36+ const response = await fetch (
37+ `${ apiUrl } /votes?videoId=${ videoId } &likeCount=` ,
38+ {
39+ method : "GET" ,
40+ headers : {
41+ Accept : "application/json" ,
42+ } ,
43+ }
44+ ) ;
45+ if ( ! response . ok ) alert ( "Error: " + response . error ) ;
46+ const response_1 = response ;
47+ return await response_1 . json ( ) ;
48+ } catch ( e ) {
49+ return alert ( "ERROR: " + e ) ;
50+ }
51+ }
52+
53+ function getVideoId ( url ) {
54+ const urlObject = new URL ( url ) ;
55+ const pathname = urlObject . pathname ;
56+ if ( pathname . startsWith ( "/clip" ) ) {
57+ return document . querySelector ( "meta[itemprop='videoId']" ) . content ;
58+ } else {
59+ if ( pathname . startsWith ( "/shorts" ) ) {
60+ return pathname . slice ( 8 ) ;
61+ }
62+ return urlObject . searchParams . get ( "v" ) ;
63+ }
64+ }
65+
66+ function isInViewport ( element ) {
67+ const rect = element . getBoundingClientRect ( ) ;
68+ const height = innerHeight || document . documentElement . clientHeight ;
69+ const width = innerWidth || document . documentElement . clientWidth ;
70+ return (
71+ // When short (channel) is ignored, the element (like/dislike AND short itself) is
72+ // hidden with a 0 DOMRect. In this case, consider it outside of Viewport
73+ ! ( rect . top == 0 && rect . left == 0 && rect . bottom == 0 && rect . right == 0 ) &&
74+ rect . top >= 0 &&
75+ rect . left >= 0 &&
76+ rect . bottom <= height &&
77+ rect . right <= width
78+ ) ;
79+ }
80+ function isVideoLoaded ( ) {
81+ const videoId = getVideoId ( window . location . href ) ;
82+ return (
83+ // desktop: spring 2024 UI
84+ document . querySelector ( `ytd-watch-grid[video-id='${ videoId } ']` ) !== null ||
85+ // desktop: older UI
86+ document . querySelector ( `ytd-watch-flexy[video-id='${ videoId } ']` ) !== null ||
87+ // mobile: no video-id attribute
88+ document . querySelector ( '#player[loading="false"]:not([hidden])' ) !== null
89+ ) ;
90+ }
91+ function createObserver ( options , callback ) {
92+ const observerWrapper = new Object ( ) ;
93+ observerWrapper . options = options ;
94+ observerWrapper . observer = new MutationObserver ( callback ) ;
95+ observerWrapper . observe = function ( element ) {
96+ this . observer . observe ( element , this . options ) ;
97+ } ;
98+ observerWrapper . disconnect = function ( ) {
99+ this . observer . disconnect ( ) ;
100+ } ;
101+ return observerWrapper ;
102+ }
103+
104+ function isShorts ( ) {
105+ return location . pathname . startsWith ( "/shorts" ) ;
106+ }
107+ function isNewDesign ( ) {
108+ return document . getElementById ( "comment-teaser" ) !== null ;
109+ }
0 commit comments