-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #100 from festoney8/dev
merge dev to main, v3.8.0
- Loading branch information
Showing
37 changed files
with
1,009 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import dynDurationFilterInstance from '../filters/subfilters/dynDuration' | ||
import dynTitleFilterInstance from '../filters/subfilters/dynTitle' | ||
import dynUploaderFilterInstance from '../filters/subfilters/dynUploader' | ||
|
||
// 代理, 接收页面操作通知, 更新子过滤器的参数 | ||
class DynUploaderFilterAgency { | ||
notifyDynUploader(event: string, value?: string | string[]) { | ||
switch (event) { | ||
case 'disable': | ||
dynUploaderFilterInstance.setStatus(false) | ||
break | ||
case 'enable': | ||
dynUploaderFilterInstance.setStatus(true) | ||
break | ||
case 'add': | ||
if (typeof value === 'string') { | ||
if (value.trim()) { | ||
dynUploaderFilterInstance.addParam(value.trim()) | ||
} | ||
} | ||
break | ||
case 'edit': | ||
if (Array.isArray(value)) { | ||
dynUploaderFilterInstance.setParams(value.map((v) => v.trim()).filter((v) => v)) | ||
} | ||
break | ||
} | ||
} | ||
notifyDynTitle(event: string, value?: string | string[]) { | ||
switch (event) { | ||
case 'disable': | ||
dynTitleFilterInstance.setStatus(false) | ||
break | ||
case 'enable': | ||
dynTitleFilterInstance.setStatus(true) | ||
break | ||
case 'add': | ||
if (typeof value === 'string') { | ||
if (value.trim()) { | ||
dynTitleFilterInstance.addParam(value.trim()) | ||
} | ||
} | ||
break | ||
case 'edit': | ||
if (Array.isArray(value)) { | ||
dynTitleFilterInstance.setParams(value.map((v) => v.trim()).filter((v) => v)) | ||
} | ||
break | ||
} | ||
} | ||
notifyDynDuration(event: string, value?: number) { | ||
switch (event) { | ||
case 'disable': | ||
dynDurationFilterInstance.setStatus(false) | ||
break | ||
case 'enable': | ||
dynDurationFilterInstance.setStatus(true) | ||
break | ||
case 'change': | ||
if (typeof value === 'number') { | ||
dynDurationFilterInstance.setParams(value) | ||
} | ||
break | ||
} | ||
} | ||
} | ||
|
||
// 单例 | ||
const dynUploaderFilterAgencyInstance = new DynUploaderFilterAgency() | ||
export default dynUploaderFilterAgencyInstance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import settings from '../../../settings' | ||
import { debugDynFilter as debug, error, log } from '../../../utils/logger' | ||
import { hideEle, isEleHide, showEle } from '../../../utils/tool' | ||
import dynDurationFilterInstance from './subfilters/dynDuration' | ||
import dynTitleFilterInstance from './subfilters/dynTitle' | ||
import dynUploaderFilterInstance from './subfilters/dynUploader' | ||
|
||
export interface IDynSubFilter { | ||
isEnable: boolean | ||
setStatus(status: boolean): void | ||
setParams?(value: string[] | number): void | ||
addParam?(value: string): void | ||
check(value: string): Promise<string> | ||
} | ||
|
||
export type DynSelectorFunc = { | ||
dynUploader?: (dyn: HTMLElement) => string | null | ||
dynDuration?: (dyn: HTMLElement) => string | null | ||
dynTitle?: (dyn: HTMLElement) => string | null | ||
} | ||
|
||
interface DynInfo { | ||
dynUploader?: string | undefined | ||
dynDuration?: string | undefined | ||
dynTitle?: string | undefined | ||
} | ||
|
||
class CoreDynFilter { | ||
/** | ||
* 对动态内容进行并发检测 | ||
* @param dyns 动态列表 | ||
* @param sign 是否标记已过滤项 | ||
* @param selectorFunc 使用selector选取元素的函数 | ||
*/ | ||
checkAll(dyns: HTMLElement[], sign = true, selectorFunc: DynSelectorFunc) { | ||
try { | ||
const checkDynUploader = dynUploaderFilterInstance.isEnable && selectorFunc.dynUploader !== undefined | ||
const checkDynDuration = dynDurationFilterInstance.isEnable && selectorFunc.dynDuration !== undefined | ||
const checkDynTitle = dynTitleFilterInstance.isEnable && selectorFunc.dynTitle !== undefined | ||
|
||
if (!checkDynUploader && !checkDynDuration && !checkDynTitle) { | ||
// 黑名单全部关闭时 恢复全部动态 | ||
dyns.forEach((dyn) => showEle(dyn)) | ||
return | ||
} | ||
|
||
dyns.forEach((dyn) => { | ||
const info: DynInfo = {} | ||
|
||
// 构建黑白名单任务, 调用各个子过滤器的check()方法检测 | ||
const blackTasks: Promise<string>[] = [] | ||
const whiteTasks: Promise<string>[] = [] | ||
|
||
if (checkDynUploader) { | ||
const dynUploader = selectorFunc.dynUploader!(dyn) | ||
if (dynUploader) { | ||
blackTasks.push(dynUploaderFilterInstance.check(dynUploader)) | ||
info.dynUploader = dynUploader | ||
} | ||
} | ||
if (checkDynDuration) { | ||
const dynDuration = selectorFunc.dynDuration!(dyn) | ||
if (dynDuration) { | ||
blackTasks.push(dynDurationFilterInstance.check(dynDuration)) | ||
info.dynDuration = dynDuration | ||
} | ||
} | ||
if (checkDynTitle) { | ||
const dynTitle = selectorFunc.dynTitle!(dyn) | ||
if (dynTitle) { | ||
blackTasks.push(dynTitleFilterInstance.check(dynTitle)) | ||
info.dynTitle = dynTitle | ||
} | ||
} | ||
|
||
// 执行检测 | ||
Promise.all(blackTasks) | ||
.then((_result) => { | ||
// 未命中黑名单 | ||
// debug(_result) | ||
showEle(dyn) | ||
Promise.all(whiteTasks) | ||
.then((_result) => {}) | ||
.catch((_result) => {}) | ||
}) | ||
.catch((_result) => { | ||
// 命中黑名单 | ||
debug(_result) | ||
if (whiteTasks.length) { | ||
Promise.all(whiteTasks) | ||
.then((_result) => { | ||
// 命中黑名单,未命中白名单 | ||
// debug(_result) | ||
if (!isEleHide(dyn)) { | ||
log(`hide dyn | ||
dynUploader: ${info.dynUploader} | ||
dynDuration: ${info.dynDuration} | ||
dynTitle: ${info.dynTitle}`) | ||
} | ||
hideEle(dyn) | ||
}) | ||
.catch((_result) => { | ||
// 命中白名单 | ||
// debug(_result) | ||
showEle(dyn) | ||
}) | ||
} else { | ||
if (!isEleHide(dyn)) { | ||
log(`hide dyn | ||
dynUploader: ${info.dynUploader} | ||
dynDuration: ${info.dynDuration} | ||
dynTitle: ${info.dynTitle}`) | ||
} | ||
hideEle(dyn) | ||
} | ||
}) | ||
|
||
// 标记已过滤动态 | ||
sign && dyn.setAttribute(settings.filterSign, '') | ||
}) | ||
} catch (err) { | ||
error(err) | ||
error('CoreDynFilter checkAll error') | ||
} | ||
} | ||
} | ||
|
||
const coreDynFilterInstance = new CoreDynFilter() | ||
export default coreDynFilterInstance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { IDynSubFilter } from '../core' | ||
|
||
class DynDurationFilter implements IDynSubFilter { | ||
// 时长阈值, 单位秒 | ||
private threshold = 0 | ||
isEnable = false | ||
|
||
setStatus(status: boolean) { | ||
this.isEnable = status | ||
} | ||
|
||
setParams(threshold: number) { | ||
this.threshold = threshold | ||
} | ||
|
||
// dynDuration转换为秒数, 支持 HH:MM:SS, MM:SS, 纯数字 | ||
dynDurationToSec = (dynDuration: string): number => { | ||
dynDuration = dynDuration.trim() | ||
if (dynDuration.match(/^(?:\d+:)?\d+:\d+$/)) { | ||
const parts = dynDuration.split(':').map((part) => parseInt(part)) | ||
if (parts.length === 3) { | ||
return parts[0] * 3600 + parts[1] * 60 + parts[2] | ||
} | ||
if (parts.length === 2) { | ||
return parts[0] * 60 + parts[1] | ||
} | ||
} else if (dynDuration.match(/^\d+$/)) { | ||
return parseInt(dynDuration) | ||
} | ||
return -1 | ||
} | ||
|
||
check(dynDuration: string): Promise<string> { | ||
return new Promise<string>((resolve, reject) => { | ||
if (!this.isEnable || this.threshold === 0) { | ||
resolve(`DynDuration resolve, disable or 0`) | ||
} else { | ||
const seconds = this.dynDurationToSec(dynDuration) | ||
if (seconds > 0 && seconds > this.threshold) { | ||
resolve(`DynDuration OK`) | ||
} else { | ||
reject(`DynDuration too short`) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// 单例 | ||
const dynDurationFilterInstance = new DynDurationFilter() | ||
export default dynDurationFilterInstance |
Oops, something went wrong.