Skip to content

Commit

Permalink
LinkHints: sed: now support per-host sed by sed: string[]
Browse files Browse the repository at this point in the history
  • Loading branch information
gdh1995 committed Jun 5, 2024
1 parent 9b7e588 commit e848ed9
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
6 changes: 4 additions & 2 deletions background/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ declare const enum SedAction {
camel = 14, camelcase = 14, dash = 15, dashed = 15, hyphen = 15, capitalize = 16, capitalizeAll = 17,
latin = 18, latinize = 18, latinise = 18, noaccent = 18, nodiacritic = 18, decodeAll = 19,
json = 20, jsonParse = 21, virtually = 22, virtual = 22, dryRun = 22,
inc = 23, increase = 23, dec = 24, decrease = 24, readableJson = 25,
inc = 23, increase = 23, dec = 24, decrease = 24, readableJson = 25, length = 26,
break = 99, stop = 99, return = 99,
}
type SedActions = SedAction | `${string}=${string}`
Expand Down Expand Up @@ -47,6 +47,7 @@ const SedActionMap: ReadonlySafeDict<SedAction> = {
json: SedAction.json, jsonparse: SedAction.jsonParse, readablejson: SedAction.readableJson,
virtual: SedAction.virtually, virtually: SedAction.virtually, dryrun: SedAction.virtually,
inc: SedAction.inc, dec: SedAction.dec, increase: SedAction.inc, decrease: SedAction.dec,
length: SedAction.length,
} satisfies SafeObject & {
[key in Exclude<keyof typeof SedAction, "NONE"> as NormalizeKeywords<key>]: (typeof SedAction)[key]
}
Expand Down Expand Up @@ -202,7 +203,7 @@ export const parseSedOptions_ = (sed: UserSedOptions): ParsedSedOpts | null => {
let r = sed.sed, k = sed.sedKeys || sed.sedKey
return r == null && (!k && k !== 0) ? null : !r || typeof r !== "object"
? sed.$sed = { r: typeof r === "number" ? r + "" : r, k: typeof k === "number" ? k + "" : k }
: r.r != null || r.k ? r : null
: !(r instanceof Array) && (r.r != null || r.k) ? r : null
}

const parseSedKeys_ = (keys: string | number | object, parsed?: ParsedSedOpts): Contexts | null => {
Expand Down Expand Up @@ -414,6 +415,7 @@ set_substitute_(((input: string, normalContext: SedContext, mixedSed?: MixedSedO
: action === SedAction.jsonParse ? tryParseJSON(text)
: action === SedAction.inc ? +text + 1 + ""
: action === SedAction.dec ? +text - 1 + ""
: action === SedAction.length ? text.length + ""
: (text = (action === SedAction.normalize || action === SedAction.reverseText || action === SedAction.latin)
&& (!OnChrome || Build.MinCVer >= BrowserVer.Min$String$$Normalize
|| text.normalize) ? text.normalize(action === SedAction.latin ? "NFD" : "NFC") : text,
Expand Down
5 changes: 3 additions & 2 deletions front/vomnibar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -973,8 +973,9 @@ var VCID_: string | undefined = VCID_ || "", VHost_: string | undefined = VHost_
navReq: Req.fg<kFgReq.openUrl> | null = useItem && item.s != null && !itemSed && !itemKeyword
? null : { H: kFgReq.openUrl, f: false, r: action, h: useItem ? null : https,
u: field && useItem ? field in item ? item[field as keyof typeof item] + "" : "" : item.u,
o: { i: options.incognito, s: useItem ? itemSed || { r: false, k: "" } : typeof inputSed === "object" && inputSed
|| { r: inputSed, k: options.inputSedKeys || options.sedKeys || options.sedKey },
o: { i: options.incognito,
s: useItem ? itemSed || { r: false, k: "" } : typeof inputSed === "object" ? inputSed instanceof Array
? null : inputSed : { r: inputSed, k: options.inputSedKeys || options.sedKeys || options.sedKey },
k: (useItem || !field) && itemKeyword || null, p: options.position,
t: useItem ? !!testUrl : testUrl != null ? testUrl : "whole" }
}, sessionReq: Req.fg<kFgReq.gotoSession> | null = navReq ? null : { H: kFgReq.gotoSession,
Expand Down
9 changes: 6 additions & 3 deletions lib/dom_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,8 @@ export const joinValidSelectors = (selector: string | false | void
|| safeCall(querySelector_unsafe_, selector, _domInst || (_domInst = createElement_("a"))) !== void 0)
? (validAnother ? selector + "," + validAnother : selector) as "css-selector" : validAnother || null

export const findSelectorByHost = (rules: string | string[] | kTip | MayBeSelector | void): "css-selector" | void => {
export const findSelectorByHost = ((rules: string | string[] | kTip | MayBeSelector | void
, noCheck?: 1): "css-selector" | void => {
const isKTip = isTY(rules, kTY.num)
let host: string | undefined, path: string | undefined
for (const arr of !rules ? [] : isTY(rules,kTY.obj) ? rules : (isKTip ? VTr(rules) : rules + "").split(";")) {
Expand All @@ -595,11 +596,13 @@ export const findSelectorByHost = (rules: string | string[] | kTip | MayBeSelect
const re = cond && (<RegExpOne> /[^*+$\\?(]/).test(cond) && tryCreateRegExp(cond)
path || cond && (host = Lower(loc_.host), path = host + "/" + Lower(loc_.pathname))
if ((re ? re.test(matchPath ? path! : host!) : matchPath ? path!.startsWith(cond)
: !cond || host === cond || host!.endsWith("." + cond)) && (isKTip || joinValidSelectors(sel, 0))) {
: !cond || host === cond || host!.endsWith("." + cond))
&& (isKTip || noCheck || joinValidSelectors(sel, 0))) {
return sel as "css-selector"
}
}
}
}) as <T extends BOOL = 0>(rules: string | string[] | kTip | MayBeSelector | void
, noCheck?: T) => T extends 1 ? string | undefined : "css-selector" | void

export const elFromPoint_ = (center?: Point2D | null, baseEl?: SafeElement | ShadowRoot | null): Element | null => {
const root = center && (baseEl ? isNode_(baseEl, kNode.DOCUMENT_FRAGMENT_NODE) ? baseEl
Expand Down
5 changes: 4 additions & 1 deletion lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { findSelectorByHost } from "./dom_utils"

export type XrayedObject<T extends object> = T & { wrappedJSObject: T }

const OnOther_: BrowserType = Build.BTypes && !(Build.BTypes & (Build.BTypes - 1))
Expand Down Expand Up @@ -216,7 +218,8 @@ export const recordLog = (tip: kTip | string): (() => void) =>

export const parseSedOptions = (opts: UserSedOptions): ParsedSedOpts => {
const sed = opts.sed
return isTY(sed, kTY.obj) && sed || { r: sed, k: opts.sedKeys || opts.sedKey }
return isTY(sed, kTY.obj) && sed ? !(sed as string[]).length ? sed as Exclude<typeof sed, string[]>
: { r: "", k: findSelectorByHost<1>(sed as string[], 1) } : { r: sed, k: opts.sedKeys || opts.sedKey }
}

type EnsureExisting<T> = { [P in keyof T]-?: T[P] }
Expand Down
2 changes: 1 addition & 1 deletion typings/messages.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ interface ParsedSedOpts {
}
type MixedSedOpts = string | number | boolean | ParsedSedOpts
interface UserSedOptions {
sed?: MixedSedOpts | null
sed?: MixedSedOpts | string[] | null
/** only in LinkHints now */ sedIf?: "regexp-for-<a>.href" | null
sedKeys?: string | number | null
sedKey?: string | number | null
Expand Down

0 comments on commit e848ed9

Please sign in to comment.