Refactor text validator

This commit is contained in:
the1812 2018-12-18 18:36:38 +08:00
parent d6020ddab8
commit 1d4ec81191
11 changed files with 175 additions and 121 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
// ==UserScript==
// @name Bilibili Evolved (Preview)
// @version 1.6.14
// @version 1.6.15
// @description 增强哔哩哔哩Web端体验(预览版分支): 修复界面瑕疵, 删除广告, 使用夜间模式浏览, 下载视频或视频封面, 以及增加对触屏设备的支持等.
// @author Grant Howard, Coulomb-G
// @copyright 2018, Grant Howrad (https://github.com/the1812)
@ -200,10 +200,14 @@
iconsStyle: {
path: "min/icons.min.css",
},
textValidate: {
path: "min/text-validate.min.js",
},
guiSettings: {
path: "min/gui-settings.min.js",
dependencies: [
"guiSettingsDom",
"textValidate",
],
styles: [
"guiSettingsStyle",

View File

@ -1,6 +1,6 @@
// ==UserScript==
// @name Bilibili Evolved
// @version 1.6.14
// @version 1.6.15
// @description 增强哔哩哔哩Web端体验: 修复界面瑕疵, 删除广告, 使用夜间模式浏览, 下载视频或视频封面, 以及增加对触屏设备的支持等.
// @author Grant Howard, Coulomb-G
// @copyright 2018, Grant Howrad (https://github.com/the1812)
@ -200,10 +200,14 @@
iconsStyle: {
path: "min/icons.min.css",
},
textValidate: {
path: "min/text-validate.min.js",
},
guiSettings: {
path: "min/gui-settings.min.js",
dependencies: [
"guiSettingsDom",
"textValidate",
],
styles: [
"guiSettingsStyle",

View File

@ -72,6 +72,7 @@ namespace BilibiliEvolved.Build
&& !file.FullName.Contains(@".vscode\")
&& !file.FullName.Contains(@"build-scripts\")
&& !file.FullName.Contains(@"node_modules\")
&& !file.FullName.Contains(@".backup.")
);
using (var cache = new BuildCache())
{

Binary file not shown.

File diff suppressed because one or more lines are too long

1
min/text-validate.min.js vendored Normal file
View File

@ -0,0 +1 @@
(()=>{return(t,e)=>{class i{constructor(t){this.key=t}get originalValue(){return t[this.key]}static getValidator(t){switch(t){case"customStyleColor":return new r(t);case"blurBackgroundOpacity":case"customControlBackgroundOpacity":return new a(t);case"defaultPlayerMode":case"defaultVideoQuality":return new s(t);case"darkScheduleStart":case"darkScheduleEnd":return new n(t);default:return new i(t)}}isValidate(t){return t}validate(t){const e=this.isValidate(t);if(e===undefined){return this.originalValue}return e}}class r extends i{isValidate(t){if(/^#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})$/.test(t)){if(t.length<7){return`#${t[1]}${t[1]}${t[2]}${t[2]}${t[3]}${t[3]}`}else{return t}}}}class a extends i{isValidate(t){if(/^([-\+]?\d+)(\.\d+)?$/.test(t)){const e=parseFloat(t);if(e>=0&&e<=1){return t}}}}class s extends i{isValidate(t){const[e]=Object.values(Resource.manifest).filter(t=>t.dropdown&&t.dropdown.key===this.key).map(t=>t.dropdown);if(e.items.indexOf(t)!==-1){return t}}}class n extends i{isValidate(t){const e=t.match(/^([\d]{1,2}):([\d]{1,2})$/);if(e&&e.length>=3){const t={hour:parseInt(e[1]),minute:parseInt(e[2])};(function(){while(this.minute<0){this.minute+=60;this.hour-=1}while(this.minute>=60){this.minute-=60;this.hour+=1}while(this.hour<0){this.hour+=24}while(this.hour>=24){this.hour-=24}}).call(t);return`${t.hour}:${t.minute<10?"0"+t.minute:t.minute}`}}}return{export:{Validator:i,ColorValidator:r,DropDownValidator:s,OpacityValidator:a,TimeValidator:n}}}})();

View File

@ -2,6 +2,7 @@
{
return (settings, resources) =>
{
const Validator = resources.attributes.textValidate.export.Validator;
const colors = {
red: "#e57373",
pink: "#F06292",
@ -26,6 +27,12 @@
return function (newColor)
{
const color = new ColorProcessor(newColor);
const shadowColor = color.hexToRgba(newColor + "70");
$("div.custom-color-preview")
.css("background", newColor)
.css("box-shadow", `0px 2px 8px 1px rgba(${shadowColor.r},${shadowColor.g},${shadowColor.b},${shadowColor.a})`);
html.style.setProperty("--theme-color", newColor);
for (let opacity = 10; opacity <= 90; opacity += 10)
{
@ -38,57 +45,7 @@
html.style.setProperty("--invert-filter", color.filterInvert);
};
})();
const textValidate = {
forceWideMinWidth: text => text, /* How to validate CSS unit ?? */
customStyleColor: text =>
{
const match = text.match(/^#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})$/);
if (match)
{
if (text.length < 7)
{
return `#${text[1]}${text[1]}${text[2]}${text[2]}${text[3]}${text[3]}`;
}
else
{
return text;
}
}
else
{
return settings.customStyleColor;
}
},
blurBackgroundOpacity: text =>
{
const match = text.match(/^([-\+]?\d+)(\.\d+)?$/);
if (match)
{
const value = parseFloat(text);
if (value >= 0 && value <= 1)
{
return text;
}
}
return settings.blurBackgroundOpacity;
},
defaultPlayerMode: text =>
{
if (Resource.manifest.useDefaultPlayerMode.dropdown.items.indexOf(text) !== -1)
{
return text;
}
return settings.defaultPlayerMode;
},
defaultVideoQuality: text =>
{
if (Resource.manifest.useDefaultVideoQuality.dropdown.items.indexOf(text) !== -1)
{
return text;
}
return settings.defaultVideoQuality;
}
};
function getCategoriyItems(category)
{
let element = category.nextElementSibling;
@ -100,57 +57,6 @@
}
return elements;
}
function darkScheduleValidate(text, defaultValue)
{
const match = text.match(/^([\d]{1,2}):([\d]{1,2})$/);
if (match && match.length >= 3)
{
const time = { hour: parseInt(match[1]), minute: parseInt(match[2]) };
(function ()
{
while (this.minute < 0)
{
this.minute += 60;
this.hour -= 1;
}
while (this.minute >= 60)
{
this.minute -= 60;
this.hour += 1;
}
while (this.hour < 0)
{
this.hour += 24;
}
while (this.hour >= 24)
{
this.hour -= 24;
}
}).call(time);
return `${time.hour}:${(time.minute < 10 ? "0" + time.minute : time.minute)}`;
}
else
{
return defaultValue;
}
}
textValidate.darkScheduleStart = text => darkScheduleValidate(text, settings.darkScheduleStart);
textValidate.darkScheduleEnd = text => darkScheduleValidate(text, settings.darkScheduleEnd);
function opacityValidate(text, defaultValue)
{
const match = text.match(/^([-\+]?\d+)(\.\d+)?$/);
if (match)
{
const value = parseFloat(text);
if (value >= 0 && value <= 1)
{
return text;
}
}
return defaultValue;
}
textValidate.blurBackgroundOpacity = text => opacityValidate(text, settings.blurBackgroundOpacity);
textValidate.customControlBackgroundOpacity = text => opacityValidate(text, settings.customControlBackgroundOpacity);
function settingsChange(key, value)
{
@ -177,14 +83,7 @@
{
$(".gui-settings-widgets-box,.gui-settings-box,.gui-settings-mask").removeClass("opened");
});
$("input[key='customStyleColor']").on("input", () =>
{
const color = textValidate.customStyleColor($("input[key='customStyleColor']").val());
const shadowColor = resources.color.hexToRgba(color + "70");
$("div.custom-color-preview")
.css("background", color)
.css("box-shadow", `0px 2px 8px 1px rgba(${shadowColor.r},${shadowColor.g},${shadowColor.b},${shadowColor.a})`);
});
$("input[type='text'][key]").each((_, element) =>
{
$(element).attr("placeholder", settings[$(element).attr("key")]);
@ -228,7 +127,7 @@
.each((_, element) =>
{
const key = element.getAttribute("key");
const value = textValidate[key](element.value);
const value = Validator.getValidator(key).validate(element.value);
if (key === "customStyleColor")
{
reloadColor(value);

View File

@ -0,0 +1,135 @@
(() =>
{
return (settings, resources) =>
{
class Validator
{
constructor(key)
{
this.key = key;
}
get originalValue()
{
return settings[this.key];
}
static getValidator(key)
{
switch (key)
{
case "customStyleColor":
return new ColorValidator(key);
case "blurBackgroundOpacity":
case "customControlBackgroundOpacity":
return new OpacityValidator(key);
case "defaultPlayerMode":
case "defaultVideoQuality":
return new DropDownValidator(key);
case "darkScheduleStart":
case "darkScheduleEnd":
return new TimeValidator(key);
default:
return new Validator(key);
}
}
isValidate(text)
{
return text;
}
validate(text)
{
const result = this.isValidate(text);
if (result === undefined)
{
return this.originalValue;
}
return result;
}
}
class ColorValidator extends Validator
{
isValidate(text)
{
if (/^#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})$/.test(text))
{
if (text.length < 7)
{
return `#${text[1]}${text[1]}${text[2]}${text[2]}${text[3]}${text[3]}`;
}
else
{
return text;
}
}
}
}
class OpacityValidator extends Validator
{
isValidate(text)
{
if (/^([-\+]?\d+)(\.\d+)?$/.test(text))
{
const value = parseFloat(text);
if (value >= 0 && value <= 1)
{
return text;
}
}
}
}
class DropDownValidator extends Validator
{
isValidate(text)
{
const [dropdownInfo] = Object.values(Resource.manifest)
.filter(it => it.dropdown && it.dropdown.key === this.key)
.map(it => it.dropdown);
if (dropdownInfo.items.indexOf(text) !== -1)
{
return text;
}
}
}
class TimeValidator extends Validator
{
isValidate(text)
{
const match = text.match(/^([\d]{1,2}):([\d]{1,2})$/);
if (match && match.length >= 3)
{
const time = { hour: parseInt(match[1]), minute: parseInt(match[2]) };
(function ()
{
while (this.minute < 0)
{
this.minute += 60;
this.hour -= 1;
}
while (this.minute >= 60)
{
this.minute -= 60;
this.hour += 1;
}
while (this.hour < 0)
{
this.hour += 24;
}
while (this.hour >= 24)
{
this.hour -= 24;
}
}).call(time);
return `${time.hour}:${(time.minute < 10 ? "0" + time.minute : time.minute)}`;
}
}
}
return {
export: {
Validator,
ColorValidator,
DropDownValidator,
OpacityValidator,
TimeValidator,
},
};
};
})();

View File

@ -1 +1 @@
1.6.14
1.6.15