1- // Globals
2- let treeWalker ;
3-
41// Searches the external post for an unblurred match, and ensures it's the same image.
52function getUnspoileredSrc ( blurredSrc , link ) {
63 return new Promise ( ( resolve , reject ) => {
74 // Checking to see if the link isn't already unblurred
85 const splitURL = link . split ( '?' ) [ 1 ]
96 const urlParams = new URLSearchParams ( splitURL ) ;
10- if ( link . split ( '/' ) [ 2 ] = = 'preview.redd.it ' && ! urlParams . has ( 'blur' ) ) {
11- resolve ( link ) ;
7+ if ( link . split ( '/' ) [ 2 ] != 'reddit.com' && link . split ( '/' ) [ 2 ] ! = 'www.reddit.com ' && ! urlParams . has ( 'blur' ) ) {
8+ return resolve ( link ) ;
129 }
1310
1411 // Getting source from external page
15- const request = new window . XMLHttpRequest ( ) ;
16- request . open ( 'GET' , link ) ;
17- request . responseType = 'document' ;
18- request . onload = ( ) => {
19- if ( request . readyState !== request . DONE && request . status !== 200 ) return ;
20-
21- // Creating temporary tree walker
22- const page = request . response ;
23- const pageContent = page . getElementById ( 'AppRouter-main-content' ) ;
24- const tmpTreeWalker = page . createTreeWalker ( pageContent , NodeFilter . SHOW_ELEMENT , ( node ) => {
25- return node . nodeName == 'A' ? NodeFilter . FILTER_ACCEPT : NodeFilter . FILTER_SKIP ;
26- } ) ;
27-
28- // Looping through to grab source
29- let curNode = tmpTreeWalker . currentNode ;
30- while ( curNode ) {
31- curNode = tmpTreeWalker . nextNode ( ) ;
32-
33- // Check Children
34- const children = curNode . childNodes ;
35- for ( let i = 0 ; i < children . length ; i ++ ) {
36- const child = children [ i ] ;
37- if ( child . nodeName !== "IMG" ) continue ;
38- if ( child . src == blurredSrc ) {
39- resolve ( curNode . href ) ;
40- return ;
12+ GM_xmlhttpRequest ( {
13+ method : 'GET' ,
14+ url : link ,
15+ responseType : 'document' ,
16+ onload : ( response ) => {
17+ if ( response . readyState !== response . DONE && response . status !== 200 ) return ;
18+
19+ // Creating temporary tree walker
20+ const page = response . response
21+ const pageContent = page . getElementById ( 'AppRouter-main-content' ) ;
22+ const tmpTreeWalker = page . createTreeWalker ( pageContent , NodeFilter . SHOW_ELEMENT , ( node ) => {
23+ return node . nodeName == 'A' ? NodeFilter . FILTER_ACCEPT : NodeFilter . FILTER_SKIP ;
24+ } ) ;
25+
26+ // Looping through to grab source
27+ let curNode = tmpTreeWalker . currentNode ;
28+ while ( curNode ) {
29+ curNode = tmpTreeWalker . nextNode ( ) ;
30+
31+ // Check Children
32+ const children = curNode . childNodes ;
33+ for ( let i = 0 ; i < children . length ; i ++ ) {
34+ const child = children [ i ] ;
35+ if ( child . nodeName ! == "IMG" ) continue ;
36+ if ( child . src == blurredSrc )
37+ return resolve ( curNode . href ) ;
4138 }
4239 }
43- }
44-
45- // Something up
46- reject ( blurredSrc ) ;
47- }
4840
49- // Sending request
50- request . send ( ) ;
41+ // Something up
42+ return reject ( blurredSrc ) ;
43+ }
44+ } ) ;
5145 } ) ;
5246}
5347
@@ -81,11 +75,10 @@ function removePostSpoiler(element, postLink) {
8175 // Parenting
8276 element . parentNode . parentNode . appendChild ( video ) ;
8377 element . parentNode . remove ( ) ;
84- treeWalker . currentNode = video ; // Fix for treeWalker freezing on deleted div
8578
8679 // Fixing styling
8780 video . parentNode . style = "" ;
88-
81+ element = video ;
8982 break ;
9083 }
9184 default : {
@@ -94,16 +87,38 @@ function removePostSpoiler(element, postLink) {
9487 break ;
9588 }
9689 }
90+
91+ // Ensuring the spoiler button doesn't show up again
92+ new MutationObserver ( ( list ) => list . filter ( mut => mut . type === 'childList' ) . forEach ( ( mut ) => {
93+ for ( let child of mut . addedNodes ) {
94+ if ( child != element ) {
95+ child . remove ( ) ;
96+ }
97+ }
98+ } ) ) . observe ( element . parentNode , { childList : true } ) ;
9799 } ) ;
98100}
99101
100- // Scans through all of the page's posts and attempts to unspoiler
101- let cooldown = false ;
102- function scanContent ( ) {
103- if ( cooldown ) return ;
104- cooldown = true ;
102+ // Searches through the post and finds the image to unspoiler
103+ function getImageFromPost ( post ) {
104+ // Creating tree walker from post
105+ const treeWalker = document . createTreeWalker ( post , NodeFilter . SHOW_ELEMENT , function ( node ) {
106+ // Validating
107+ if ( node . nodeName == 'IMG' ) {
108+ // Constructing URL
109+ const url = node . src ;
110+ const splitURL = url . split ( '?' ) [ 1 ]
111+ const urlParams = new URLSearchParams ( splitURL ) ;
112+
113+ // Checking to see if it's a valid post
114+ if ( url . split ( '/' ) [ 2 ] == 'preview.redd.it' && urlParams . has ( 'blur' ) ) {
115+ return NodeFilter . FILTER_ACCEPT ;
116+ }
117+ }
118+
119+ return NodeFilter . FILTER_SKIP ;
120+ } ) ;
105121
106- // Loop through the nodes
107122 let currentNode = treeWalker . currentNode ;
108123 while ( currentNode ) {
109124 // Getting next node along
@@ -122,45 +137,23 @@ function scanContent() {
122137 if ( par . nodeName != 'A' ) continue ;
123138 removePostSpoiler ( currentNode , par . href ) ;
124139 }
125-
126- setTimeout ( ( ) => cooldown = false , 500 ) ;
127140}
128141
142+ /*===============*/
129143
130- /* Initialises and sets up post unspoilerinator */
131-
132- // Getting the root node
133- let rootNode ;
134144if ( window . location . href . split ( '/' ) [ 5 ] !== 'comments' ) {
145+ // Attempting to dynamically grab the root
135146 let depth = 0 ;
136- rootNode = document . getElementsByClassName ( 'scrollerItem Post' ) [ 0 ] . parentNode . parentNode . parentNode ;
147+ const rootNode = document . getElementsByClassName ( 'scrollerItem Post' ) [ 0 ] . parentNode . parentNode . parentNode ;
137148 while ( rootNode . childNodes . length <= 5 && depth < 10 ) {
138149 rootNode = rootNode . parentNode ;
139150 depth ++ ;
140151 }
141152
153+ /* Detecting newly added posts, so we can unspoiler them */
154+ new MutationObserver ( ( mutationList ) => mutationList . filter ( mut => mut . type === 'childList' ) . forEach ( ( mut ) => {
155+ getImageFromPost ( mut . addedNodes [ 0 ] ) ;
156+ } ) ) . observe ( rootNode , { childList : true } ) ; // old method: window.addEventListener('scroll', scan)
142157} else {
143- rootNode = document . getElementsByClassName ( 'Post' ) [ 0 ] . parentNode ;
144- }
145-
146- // Create tree walker
147- treeWalker = document . createTreeWalker ( rootNode , NodeFilter . SHOW_ELEMENT , function ( node ) {
148- // Validating
149- if ( node . nodeName == 'IMG' ) {
150- // Constructing URL
151- const url = node . src ;
152- const splitURL = url . split ( '?' ) [ 1 ]
153- const urlParams = new URLSearchParams ( splitURL ) ;
154-
155- // Checking to see if it's a valid post
156- if ( url . split ( '/' ) [ 2 ] == 'preview.redd.it' && urlParams . has ( 'blur' ) ) {
157- return NodeFilter . FILTER_ACCEPT ;
158- }
159- }
160-
161- return NodeFilter . FILTER_SKIP ;
162- } ) ;
163-
164- // Setup content scanner
165- scanContent ( )
166- new MutationObserver ( scanContent ) . observe ( rootNode , { childList : true } ) ; // old method: window.addEventListener('scroll', scan)
158+ getImageFromPost ( document . getElementsByClassName ( 'Post' ) [ 0 ] ) ;
159+ }
0 commit comments