MediaWiki:Group-user.js: Difference between revisions

Content deleted Content added
Temporary counter for rough approximation of different screen and window widths
Tag: Reverted
migrate from MediaWiki:Common.js
 
(One intermediate revision by one other user not shown)
Line 24:
}
}
/* Actions specific to the edit page */
});
if ( mw.config.get( 'wgAction' ) === 'edit' || mw.config.get( 'wgAction' ) === 'submit' ) {
 
/**
// Temporary counter for rough approximation of different screen and window width usage
* Fix edit summary prompt for undo
// -- Krinkle, 12 October 2022.
*
//
* Fixes the fact that the undo function combined with the "no edit summary prompter"
// Test Plan:
* complains about missing editsummary, if leaving the edit summary unchanged.
// - Open maxized window with default zoom level at https://en.wikipedia.org/
* Added by [[User:Deskana]], code by [[User:Tra]].
// - In devtools console, confirm `computeWidthUsageMetric();` returns
* See also [[phab:T10912]].
// something like ".le_<logical screen width>.le_100_pc".
*/
// - Zoom out to 50%, confirm returns the same 100% width.
$( function () {
// - Zoom in to 150%, confirm returns the same 100% width.
if ( document.___location.search.indexOf( 'undo=' ) !== -1 && document.getElementsByName( 'wpAutoSummary' )[ 0 ] ) {
// - Resize to half width, confirm returns ".le_<same screen width>.le_50_pc".
document.getElementsByName( 'wpAutoSummary' )[ 0 ].value = '1';
// - Zoom in to 150%, confirm returns the same 50% width.
// - Zoom out to 150%, confirm returns the same 50% width.
//
// Verified:10
// - Firefox 105 on macOS 10.15 Catalina (2dppx)
// - Safari 15 on macOS 10.15 (2dppx)
// - Chrome 103 on macOS 10.15 (2dppx)
// - Chrome 106 on Windows 8 (1dppx)
// - Edge 105 on Windows 8 (1dppx)
// - Edge 105 on Windows 11 (1dppx)
// - Opera 91 on Windows 11 (1dppx)
// - Opera 85 on macOS 11 Big Sur (1dppx)
// - Mobile Chrome on Android 12
// - Mobile Safari on iOS 15
( function () {
function computeWidthUsageMetric() {
function matchFirstLe(num, ranges) {
for (var i = 0; i < ranges.length; i++) {
if (num <= ranges[i]) {
return ranges[i];
}
}
} );
function getZoomLevel() {
// This only works for WebKit-based and Chromium-based browsers.
// That's fine since the bug (screen and document width being reported
// inconsistently) only exists in those browsers.
//
// Test Plan:
// - Open maxized window with default zoom level at https://en.wikipedia.org/
// - In devtools console, confirm `[window.screen.width, document.body.scrollWidth]`
// returns the effective screen resolution twice,
// and confirm that $.client.profile().layout !== 'gecko';
// - Zoom out to 50%, confirm the second number is bigger now, and the below
// snippet returns 0.50.
// - Zoom in to 150%, confirm the second number is smaller now, and the below
// snippet returns 1.50.
//
// Verified:
// - Firefox 105 on macOS (2dppx)
// - Safari 15 on macOS (2dppx)
// - Chrome 103 on macOS (2dppx)
// - Chrome 106 on Windows 8 (1dppx)
// - Edge 105 on Windows 8 (1dppx)
// - Mobile Chrome on Android 12 with Samsung Galaxy S22 (3dppx)
// - Mobile Safari on iOS 15 with iPhone 13 (3ddpx)
return Math.round(((window.outerWidth) / window.innerWidth) * 100) / 100;
}
var screenWidth = window.screen && window.screen.width;
var windowWidth = document.body && document.body.clientWidth;
var screenBucket, windowBucket;
if (!screenWidth || !windowWidth) {
screenBucket = 'n_a';
windowBucket = 'n_a';
} else if (('orientation' in window) || mw.config.get('wgMFMode')) {
screenBucket = 'mobile';
windowBucket = 'mobile';
} else {
if ($.client.profile().layout !== 'gecko') {
windowWidth = Math.round(windowWidth * getZoomLevel());
}
screenBucket = 'le_' + matchFirstLe(screenWidth, [1280, 1440, 2560, 4096, Infinity]);
windowBucket = 'le_' +
Math.round(matchFirstLe(windowWidth / screenWidth, [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, Infinity]) * 100) +
'_pc';
}
 
return 'mw.js.width_usage_2022.' + screenBucket + '.' + windowBucket;
}
});
 
function sendWidthUsageMetric() {
mw.requestIdleCallback(function() {
mw.track( 'counter.' + computeWidthUsageMetric(), 1 );
});
}
 
if (mw.config.get('wgUserName') && mw.config.get('skin') === 'vector') {
mw.loader.using(['jquery.client']).then(sendWidthUsageMetric);
}
}());