mirror of
https://github.com/kkapsner/CanvasBlocker
synced 2025-07-04 20:46:39 +02:00
Code cleanup: reduce block nesting
This commit is contained in:
parent
10413a89c3
commit
abdb95b815
6 changed files with 310 additions and 293 deletions
|
@ -206,6 +206,50 @@
|
|||
objectGetters: [function(window){return window.MediaQueryList && window.MediaQueryList.prototype;}],
|
||||
name: "matches",
|
||||
getterGenerator: function(checker){
|
||||
function getAlteredMedia(originalMedia, prefs, window){
|
||||
const dimensions = getScreenDimensions(prefs, window);
|
||||
return originalMedia.replace(
|
||||
/\(\s*(?:(min|max)-)?device-(width|height):\s+(\d+\.?\d*)px\s*\)/,
|
||||
function(m, type, dimension, value){
|
||||
value = parseFloat(value);
|
||||
let newCompareValue = value;
|
||||
switch (type){
|
||||
case "min":
|
||||
if (value <= dimensions[dimension]){
|
||||
newCompareValue = 0;
|
||||
}
|
||||
else {
|
||||
newCompareValue = 2 * physical[dimension];
|
||||
}
|
||||
break;
|
||||
case "max":
|
||||
if (value >= dimensions[dimension]){
|
||||
newCompareValue = 2 * physical[dimension];
|
||||
}
|
||||
else {
|
||||
newCompareValue = 0;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (
|
||||
Math.round(value * 100) ===
|
||||
Math.round(dimensions[dimension] * 100)
|
||||
){
|
||||
newCompareValue = physical[dimension];
|
||||
}
|
||||
else {
|
||||
newCompareValue = 0;
|
||||
}
|
||||
}
|
||||
return "(" + (type? type + "-": "") +
|
||||
"device-" + dimension + ": " +
|
||||
(
|
||||
newCompareValue /
|
||||
window.devicePixelRatio
|
||||
) + "px)";
|
||||
}
|
||||
);
|
||||
}
|
||||
const temp = {
|
||||
get matches(){
|
||||
return checkerWrapper(checker, this, arguments, function(args, check){
|
||||
|
@ -219,49 +263,8 @@
|
|||
) &&
|
||||
this.media.match(/device-(width|height)/)
|
||||
){
|
||||
const dimensions = getScreenDimensions(prefs, window);
|
||||
const originalMedia = this.media;
|
||||
const alteredMedia = this.media.replace(
|
||||
/\(\s*(?:(min|max)-)?device-(width|height):\s+(\d+\.?\d*)px\s*\)/,
|
||||
function(m, type, dimension, value){
|
||||
value = parseFloat(value);
|
||||
let newCompareValue = value;
|
||||
switch (type){
|
||||
case "min":
|
||||
if (value <= dimensions[dimension]){
|
||||
newCompareValue = 0;
|
||||
}
|
||||
else {
|
||||
newCompareValue = 2 * physical[dimension];
|
||||
}
|
||||
break;
|
||||
case "max":
|
||||
if (value >= dimensions[dimension]){
|
||||
newCompareValue = 2 * physical[dimension];
|
||||
}
|
||||
else {
|
||||
newCompareValue = 0;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (
|
||||
Math.round(value * 100) ===
|
||||
Math.round(dimensions[dimension] * 100)
|
||||
){
|
||||
newCompareValue = physical[dimension];
|
||||
}
|
||||
else {
|
||||
newCompareValue = 0;
|
||||
}
|
||||
}
|
||||
return "(" + (type? type + "-": "") +
|
||||
"device-" + dimension + ": " +
|
||||
(
|
||||
newCompareValue /
|
||||
window.devicePixelRatio
|
||||
) + "px)";
|
||||
}
|
||||
);
|
||||
const alteredMedia = getAlteredMedia(originalMedia, prefs, window);
|
||||
if (alteredMedia !== originalMedia){
|
||||
const alteredQuery = window.matchMedia(alteredMedia);
|
||||
const fakedValue = original.call(alteredQuery);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue