Debian Codegraph @

extensions/MultimediaViewer/resources/mmv/logging/mmv.logging.ViewLogger.js

raw symbols SCIP
1 /*
2 * This file is part of the MediaWiki extension MultimediaViewer.
3 *
4 * MultimediaViewer is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * MultimediaViewer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with MultimediaViewer. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 const { recordVirtualViewBeaconURI } = require( '../config.json' );
19
20 /**
21 * Tracks how long users are viewing images for
22 */
23 class ViewLogger {
24 /**
25 * @param {Object} windowObject Browser window object
26 */
27 constructor( windowObject ) {
28 /**
29 * Was the last image view logged or was logging skipped?
30 *
31 * @property {boolean}
32 */
33 this.wasLastViewLogged = false;
34
35 /**
36 * Record when the user started looking at the current image
37 *
38 * @property {number}
39 */
40 this.viewStartTime = 0;
41
42 /**
43 * How long the user has been looking at the current image
44 *
45 * @property {number}
46 */
47 this.viewDuration = 0;
48
49 /**
50 * The image URL to record a virtual view for
51 *
52 * @property {string}
53 */
54 this.url = '';
55
56 /**
57 * Browser window
58 *
59 * @property {Object}
60 */
61 this.window = windowObject;
62 }
63
64 /**
65 * Tracks the unview event of the current image if appropriate
66 */
67 unview() {
68 if ( !this.wasLastViewLogged ) {
69 return;
70 }
71
72 this.wasLastViewLogged = false;
73 }
74
75 /**
76 * Starts recording a viewing window for the current image
77 */
78 startViewDuration() {
79 this.viewStartTime = Date.now();
80 }
81
82 /**
83 * Stops recording the viewing window for the current image
84 */
85 stopViewDuration() {
86 if ( this.viewStartTime ) {
87 this.viewDuration += Date.now() - this.viewStartTime;
88 this.viewStartTime = 0;
89 }
90 }
91
92 /**
93 * Records the amount of time the current image has been viewed
94 */
95 recordViewDuration() {
96 let url;
97
98 this.stopViewDuration();
99
100 if ( recordVirtualViewBeaconURI ) {
101 try {
102 url = new URL( recordVirtualViewBeaconURI, location );
103 url.searchParams.set( 'duration', this.viewDuration );
104 url.searchParams.set( 'uri', this.url );
105 } catch ( e ) {
106 // the URI is malformed. We cannot log it.
107 return;
108 }
109
110 try {
111 navigator.sendBeacon( url.toString() );
112 } catch ( e ) {
113 $.ajax( {
114 type: 'HEAD',
115 url: url.toString()
116 } );
117 }
118
119 mw.log( 'Image has been viewed for ', this.viewDuration );
120 }
121
122 this.viewDuration = 0;
123
124 this.unview();
125 }
126
127 /**
128 * Sets up the view tracking for the current image
129 *
130 * @param {string} url URL of the image to record a virtual view for
131 */
132 attach( url ) {
133 this.url = url;
134 this.startViewDuration();
135
136 $( this.window )
137 .off( '.mmv-view-logger' )
138 .on( 'beforeunload.mmv-view-logger', () => this.recordViewDuration() )
139 .on( 'focus.mmv-view-logger', () => this.startViewDuration() )
140 .on( 'blur.mmv-view-logger', () => this.stopViewDuration() );
141 }
142
143 /*
144 * Stops listening to events
145 */
146 unattach() {
147 $( this.window ).off( '.mmv-view-logger' );
148 this.stopViewDuration();
149 }
150
151 /**
152 * Tracks whether or not the image view event was logged or not (i.e. was it in the logging sample)
153 *
154 * @param {boolean} wasEventLogged Whether the image view event was logged
155 */
156 setLastViewLogged( wasEventLogged ) {
157 this.wasLastViewLogged = wasEventLogged;
158 }
159 }
160
161 module.exports = ViewLogger;