11// ==UserScript==
22// @name WFES - Base
33// @namespace https://gitlab.com/fotofreund0815/WFES
4- // @version 0.1.0
4+ // @version 0.2.1
55// @description basic functionality for WFES
66// @author fotofreund0815
77// @match https://wayfarer.nianticlabs.com/*
1212( function ( ) {
1313 'use strict' ;
1414
15+ /* WFES data structures */
1516 const PREFIX = '/api/v1/vault/' ;
17+ const sStoreReview = 'wfes_Reviews' ;
1618
1719 window . wfes = { } ;
1820 window . wfes . showcase = { } ;
21+ window . wfes . review = { } ;
22+ window . wfes . review . decision = { } ;
1923
20- // window.wfes.showcase;
24+ /* overwrite XHR */
2125
22- let openOrig = window . XMLHttpRequest . prototype . open ,
26+ let openOrig = window . XMLHttpRequest . prototype . open ,
2327 sendOrig = window . XMLHttpRequest . prototype . send ;
2428
2529 function openReplacement ( method , url , async , user , password ) {
2630 this . _url = url ;
27- console . log ( "WFES OPEN: " , method , url ) ;
31+ this . _method = method ;
32+ // console.log( "WFES OPEN: ", method, url );
2833 this . addEventListener ( 'load' , handleLoadEvent ) ;
2934 return openOrig . apply ( this , arguments ) ;
30- }
35+ }
3136
32- function sendReplacement ( data ) {
33- if ( this . onreadystatechange ) {
34- this . _onreadystatechange = this . onreadystatechange ;
35- }
36- //console.log( "WFES SEND: ", data );
37- //console.dir( data );
38- // this.onreadystatechange = onReadyStateChangeReplacement;
39- return sendOrig . apply ( this , arguments ) ;
37+ function sendReplacement ( daten ) {
38+ let candidate , json ;
39+ // handle only POST requests
40+ if ( 'POST' === this . _method ) {
41+ switch ( this . _url ) {
42+ case PREFIX + 'review' :
43+ json = JSON . parse ( daten ) ;
44+ candidate = window . wfes . review . sessionHist [ json . id ] ;
45+ window . wfes . review . decision . candidate = candidate ;
46+ window . wfes . review . decision . decision = json ;
47+ window . dispatchEvent ( new Event ( "WFESReviewDecisionSent" ) ) ;
48+ break ;
49+ case PREFIX + 'skip' :
50+ json = JSON . parse ( daten ) ;
51+ candidate = window . wfes . review . sessionHist [ json . id ] ;
52+ window . wfes . review . decision . candidate = candidate ;
53+ json . skipped = true ;
54+ window . wfes . review . decision . decision = json ;
55+ window . dispatchEvent ( new Event ( "WFESReviewDecisionSent" ) ) ;
56+ break ;
57+ default :
58+ break ;
59+ }
4060 }
61+ return sendOrig . apply ( this , arguments ) ;
62+ }
4163
4264 function handleLoadEvent ( e ) {
4365 let json ;
4971 window . wfes . showcase . list = json . result . showcase ;
5072 window . dispatchEvent ( new Event ( "WFESHomePageLoaded" ) ) ;
5173 break ;
74+ case PREFIX + 'review' :
75+ if ( 'GET' === this . _method ) {
76+ json = JSON . parse ( response ) ;
77+ handleReviewData ( json . result ) ;
78+ }
79+ break ;
80+ default :
81+ break ;
5282 }
5383 } catch ( e ) {
54- console . log ( e ) ;
84+ console . warn ( e ) ;
5585 }
5686 }
5787
58- /*
59- function onReadyStateChangeReplacement() {
60- // PLACE HERE YOUR CODE FOR READYSTATECHANGE
61- if(this._onreadystatechange) {
62- return this._onreadystatechange.apply(this, arguments);
63- }
64- }
65- */
66-
6788 window . XMLHttpRequest . prototype . open = openReplacement ;
6889 window . XMLHttpRequest . prototype . send = sendReplacement ;
90+
91+ /* handle data */
92+
93+ //Useful to make comparing easier. Essentially this function iterates over all items
94+ //and uses it's unique ID as key and stores relevant values under that key.
95+ //This way on checking we can simply find the ID when looking at a current item
96+ function makeIDbasedDictionary ( itemList ) {
97+ let dict = { } ;
98+ for ( let i = 0 ; i < itemList . length ; i ++ ) {
99+ let item = itemList [ i ] ;
100+ dict [ item . id ] = item ;
101+ }
102+ return dict ;
103+ }
104+
105+ function handleReviewData ( result ) {
106+ // save review data in ...pagedata and sessionstore
107+ let reviewSessionHist = JSON . parse ( sessionStorage . getItem ( sStoreReview ) ) || [ ]
108+ window . wfes . review . sessionHist = makeIDbasedDictionary ( reviewSessionHist ) ;
109+
110+ if ( undefined === window . wfes . review . sessionHist [ result . id ] ) {
111+ reviewSessionHist . push ( result ) ;
112+ sessionStorage . setItem ( sStoreReview , JSON . stringify ( reviewSessionHist ) ) ;
113+ window . wfes . review . sessionHist [ result . id ] = result ;
114+ }
115+
116+ window . wfes . review . pageData = result ;
117+ window . dispatchEvent ( new Event ( "WFESReviewPageLoaded" ) ) ;
118+ }
119+
120+ /* we are done :-) */
69121 console . log ( "WFES BASE loaded" ) ;
70122} ) ( ) ;
0 commit comments