Greasy Fork is available in English.
Base library for my scripts
当前为
此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.icu/scripts/375557/653847/Brazenvoid%27s%20Base%20Resource.js
// ==UserScript==
// @name Brazenvoid's Base Resource
// @namespace brazenvoid
// @version 1.0.0
// @author brazenvoid
// @license GPL-3.0-only
// @description Base library for my scripts
// @run-at document-end
// ==/UserScript==
class Filters {
constructor (logger, statisticsRecorder) {
this.blacklist = []
this.iFrameContainers = []
this.logger = logger
this.sanitizationRules = []
this.statisticsRecorder = statisticsRecorder
}
init () {
for (let i = 0; i < this.blacklist.length; i++) {
this.blacklist[i] = new RegExp(this.blacklist[i], 'ig')
}
for (const substitute in this.sanitizationRules) {
for (let i = 0; i < this.sanitizationRules[substitute].length; i++) {
this.sanitizationRules[substitute][i] = new RegExp(this.sanitizationRules[substitute][i], 'ig')
}
}
}
iFramesRemover () {
let iFramesFilter = function (target) {
let iFrames = target.getElementsByTagName('iFrame')
for (let iFrame of iFrames) {
iFrame.remove()
}
logAction('IFrames removal.')
this.statisticsRecorder.record('IFrames Remover', true, iFrames.length)
}
let iFramesObserver = new ChildObserver(iFramesFilter)
for (let iFrameContainer of this.iFrameContainers) {
iFramesObserver.observe(iFrameContainer)
}
}
sanitize (string) {
for (const substitute in this.sanitizationRules) {
for (const subject of this.sanitizationRules[substitute]) {
string = string.replace(subject, substitute)
}
}
return string
}
validateBlackList (string) {
let validationCheck = true
if (this.blacklist.length > 0) {
for (const blacklistedWord of this.blacklist) {
validationCheck = string.match(blacklistedWord) === null
if (!validationCheck) {
break
}
}
this.logger.logValidation('Blacklist', validationCheck)
this.statisticsRecorder.record('blacklisted', validationCheck)
}
return validationCheck
}
validateRange (type, value, bounds) {
let validationCheck = true
if (bounds[0] > 0) {
validationCheck = value >= bounds[0]
}
if (bounds[1] > 0) {
validationCheck = value <= bounds[1]
}
this.logger.logValidation(type, validationCheck)
this.statisticsRecorder.record(type, validationCheck)
return validationCheck
}
}
class Logger {
constructor (enableDebugging) {
this.enableDebugging = enableDebugging
}
_log (message) {
if (this.enableDebugging) {
console.log(message)
}
}
logTaskCompletion (task) {
this._log('Completed: ' + task)
this.logSeparator()
}
logSeparator () {
this._log('------------------------------------------------------------------------------------')
}
logValidation (filterName, validationResult = null) {
this._log('Satisfies ' + filterName + ' Filter: ' + (validationResult ? 'true' : 'false'))
}
logVideoCheck (videoName) {
this._log('Checking Video: ' + videoName)
}
}
class ChildObserver {
constructor (handler) {
this.config = {
attributes: false,
childList: true,
subtree: false
}
this.handler = handler
this.observer = new MutationObserver(function (mutations) {
for (let mutation of mutations) {
this.handler(mutation.target)
}
})
}
observe (node) {
this.handler(node)
this.observer.observe(node, this.config)
}
}
class SelectorGenerator {
constructor (selectorPrefix) {
this.selectorPrefix = selectorPrefix
}
getSelector (selector) {
return this.selectorPrefix + selector
};
getStatLabelSelector (type) {
return this.selectorPrefix(type + '-stat')
};
}
class StatisticsRecorder {
constructor (selectorGenerator) {
this.selectorGenerator = selectorGenerator
this.statistics = []
}
record (type, validationResult, value = 1) {
if (!validationResult) {
if (typeof this.statistics[type] !== 'undefined') {
this.statistics[type] += value
} else {
this.statistics[type] = value
}
this.statistics.total += value
}
}
reset () {
this.statistics = {total: 0}
}
updateUI () {
let statLabel, UINode
for (const statisticType in this.statistics) {
UINode = this.selectorGenerator.getStatLabelSelector(statisticType)
statLabel = document.getElementById(UINode)
statLabel.textContent = this.statistics[statisticType]
}
}
}
class UIGenerator {
constructor (showUI, selectorGenerator) {
this.selectorGenerator = selectorGenerator
this.showUI = showUI
}
createFormButton (caption, onClick) {
let button = document.createElement('button')
button.textContent = caption
button.style.height = '30px'
button.style.width = '100%'
button.addEventListener('click', onClick)
return button
}
createFormGroup () {
let divFormGroup = document.createElement('div')
divFormGroup.style.display = 'block'
divFormGroup.style.height = '18px'
divFormGroup.style.marginBottom = '2px'
divFormGroup.style.padding = '5px 0'
return divFormGroup
}
createFormGroupLabel (label, inputID) {
let labelFormGroup = document.createElement('label')
labelFormGroup.style.float = 'left'
labelFormGroup.style.padding = '2px 0'
labelFormGroup.setAttribute('for', this.selectorGenerator.getSelector(inputID))
labelFormGroup.textContent = label + ': '
return labelFormGroup
}
createFormGroupStatLabel (statisticType) {
let labelFormGroup = document.createElement('label')
labelFormGroup.id = this.selectorGenerator.getStatLabelSelector(statisticType)
labelFormGroup.style.float = 'right'
labelFormGroup.style.padding = '2px 0'
labelFormGroup.textContent = '0'
return labelFormGroup
}
createFormGroupInput (id, defaultValue) {
let inputFormGroup = document.createElement('input')
inputFormGroup.id = this.selectorGenerator.getSelector(id)
inputFormGroup.style.float = 'right'
inputFormGroup.style.width = '100px'
inputFormGroup.style.textAlign = 'center'
inputFormGroup.value = defaultValue
return inputFormGroup
}
createSection (id, backgroundColor = '#ffa31a') {
let section = document.createElement('section')
section.id = this.selectorGenerator.getSelector(id)
section.style.color = 'black'
section.style.display = this.showUI ? 'block' : 'none'
section.style.fontWeight = 'bold'
section.style.position = 'fixed'
section.style.top = '250px'
section.style.left = '0'
section.style.width = '200px'
section.style.padding = '5px 10px'
section.style.backgroundColor = backgroundColor
section.style.zIndex = '1000'
}
createSeparator () {
let separator = document.createElement('hr')
separator.style.margin = '5px 5px 0 5px'
return separator
}
createSettingsFormGroup (label, defaultValue) {
let divFormGroup = this.createFormGroup()
let labelFormGroup = this.createFormGroupLabel(label, label)
let inputFormGroup = this.createFormGroupInput(label, defaultValue)
divFormGroup.appendChild(labelFormGroup)
divFormGroup.appendChild(inputFormGroup)
return divFormGroup
}
createStatisticsFormGroup (label) {
let divFormGroup = this.createFormGroup()
let labelFormGroup = this.createFormGroupLabel('Filtered ' + label + ' Videos', label)
let statLabelFormGroup = this.createFormGroupStatLabel(label)
divFormGroup.appendChild(labelFormGroup)
divFormGroup.appendChild(statLabelFormGroup)
return divFormGroup
}
}