@@ -130,11 +130,8 @@ window.QPixel = {
130130 return QPixel . _pendingUserResponse ;
131131 }
132132
133- const myselfPromise = fetch ( '/users/me' , {
134- credentials : 'include' ,
135- headers : {
136- 'Accept' : 'application/json'
137- }
133+ const myselfPromise = QPixel . fetch ( '/users/me' , {
134+ headers : { 'Accept' : 'application/json' }
138135 } ) ;
139136
140137 QPixel . _pendingUserResponse = myselfPromise ;
@@ -219,12 +216,7 @@ window.QPixel = {
219216 filters : async ( ) => {
220217 if ( this . _filters == null ) {
221218 // If they're still absent after loading from storage, load from the API.
222- const resp = await fetch ( '/users/me/filters' , {
223- credentials : 'include' ,
224- headers : {
225- 'Accept' : 'application/json'
226- }
227- } ) ;
219+ const resp = await QPixel . getJSON ( '/users/me/filters' ) ;
228220 const data = await resp . json ( ) ;
229221
230222 QPixel . Storage ?. set ( 'user_filters' , data ) ;
@@ -240,13 +232,8 @@ window.QPixel = {
240232 if ( ! user ) {
241233 return '' ;
242234 }
243-
244- const resp = await fetch ( `/users/me/filters/default?category=${ categoryId } ` , {
245- credentials : 'include' ,
246- headers : {
247- 'Accept' : 'application/json'
248- }
249- } ) ;
235+
236+ const resp = await QPixel . getJSON ( `/users/me/filters/default?category=${ categoryId } ` ) ;
250237
251238 const data = await resp . json ( ) ;
252239 return data . name ;
@@ -313,12 +300,7 @@ window.QPixel = {
313300 } ,
314301
315302 _fetchPreferences : async ( ) => {
316- const resp = await fetch ( '/users/me/preferences' , {
317- credentials : 'include' ,
318- headers : {
319- 'Accept' : 'application/json'
320- }
321- } ) ;
303+ const resp = await QPixel . getJSON ( '/users/me/preferences' ) ;
322304 const data = await resp . json ( ) ;
323305 QPixel . _updatePreferencesLocally ( data ) ;
324306 } ,
@@ -343,41 +325,56 @@ window.QPixel = {
343325 return [ currentSequence , posInSeq ] ;
344326 } ,
345327
346- fetchJSON : async ( uri , data , options ) => {
328+ fetch : async ( uri , init ) => {
347329 const defaultHeaders = {
348330 'X-CSRF-Token' : QPixel . csrfToken ( ) ,
331+ // X-Requested-With is necessary for request.xhr? to work
332+ 'X-Requested-With' : 'XMLHttpRequest' ,
349333 'Content-Type' : 'application/json' ,
350334 } ;
351335
352- const { headers = { } , ...otherOptions } = options ?? { } ;
336+ const { headers = { } , ...restInit } = init ?? { } ;
337+
338+ /** @type {RequestInit } */
339+ const requestInit = {
340+ headers : {
341+ ...defaultHeaders ,
342+ ...headers ,
343+ } ,
344+ credentials : 'include' ,
345+ ...restInit ,
346+ } ;
353347
348+ return fetch ( uri , requestInit ) ;
349+ } ,
350+
351+ fetchJSON : async ( uri , data , options = { } ) => {
354352 /** @type {RequestInit } */
355353 const requestInit = {
356354 method : 'POST' ,
357- headers : {
358- ...defaultHeaders ,
359- ...headers ,
360- } ,
361- credentials : 'include' ,
362- body : otherOptions . method === 'GET' ? void 0 : JSON . stringify ( data ) ,
363- ...otherOptions ,
355+ body : options . method === 'GET' ? void 0 : JSON . stringify ( data ) ,
356+ ...options ,
364357 } ;
365358
366- return fetch ( uri , requestInit ) ;
359+ return QPixel . fetch ( uri , requestInit ) ;
367360 } ,
368361
369362 getJSON : async ( uri , options = { } ) => {
363+ const { headers = { } } = options ?? { } ;
364+
370365 return QPixel . fetchJSON ( uri , { } , {
371366 ...options ,
367+ headers : {
368+ 'Accept' : 'application/json' ,
369+ 'X-Requested-With' : 'XMLHttpRequest' ,
370+ ...headers ,
371+ } ,
372372 method : 'GET' ,
373373 } ) ;
374374 } ,
375375
376376 getComment : async ( id ) => {
377- const resp = await fetch ( `/comments/${ id } ` , {
378- credentials : 'include' ,
379- headers : { 'Accept' : 'application/json' }
380- } ) ;
377+ const resp = await QPixel . getJSON ( `/comments/${ id } ` ) ;
381378
382379 const data = await resp . json ( ) ;
383380
@@ -392,7 +389,7 @@ window.QPixel = {
392389 url . searchParams . append ( 'inline' , `${ inline } ` ) ;
393390 url . searchParams . append ( 'show_deleted_comments' , `${ showDeleted ? 1 : 0 } ` ) ;
394391
395- const resp = await fetch ( url . toString ( ) , {
392+ const resp = await QPixel . fetch ( url . toString ( ) , {
396393 headers : { 'Accept' : 'text/html' }
397394 } ) ;
398395
@@ -404,7 +401,7 @@ window.QPixel = {
404401 getThreadsListContent : async ( id ) => {
405402 const url = new URL ( `/comments/post/${ id } ` , window . location . origin ) ;
406403
407- const resp = await fetch ( url . toString ( ) , {
404+ const resp = await QPixel . fetch ( url . toString ( ) , {
408405 headers : { 'Accept' : 'text/html' }
409406 } ) ;
410407
0 commit comments