-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add regex support for webkit patching (#147)
* feat: Better webkit patching #127 * chore: Make artifact CI (#140) * chore: Linux CLI bug fixes * chore: Update plugin JSON schema * chore: Update TODO * chore: Fix artifact build scripts
- Loading branch information
Showing
8 changed files
with
140 additions
and
57 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { Millennium } from "millennium-lib"; | ||
import { ConditionalControlFlowType, Theme, ThemeItem } from "./types"; | ||
import { constructThemePath } from "./patcher/Dispatch"; | ||
|
||
interface WebkitItem { | ||
matchString: string, | ||
targetPath: string, | ||
fileType: ConditionalControlFlowType | ||
} | ||
|
||
const ParseMatchAndTarget = (theme: Theme): WebkitItem[] => { | ||
|
||
let webkitItems: WebkitItem[] = [] | ||
|
||
// Add condition keys into the webkitItems array | ||
for (const item in theme?.Conditions) { | ||
const condition = theme.Conditions[item] | ||
|
||
for (const value in condition?.values) { | ||
const controlFlow = condition.values[value] | ||
|
||
|
||
for (const control in controlFlow?.TargetCss?.affects) { | ||
const matchString = controlFlow.TargetCss.affects[control] | ||
const targetPath = controlFlow.TargetCss.src | ||
|
||
webkitItems.push({ matchString, targetPath, fileType: ConditionalControlFlowType.TargetCss }) | ||
} | ||
|
||
for (const control in controlFlow?.TargetJs?.affects) { | ||
const matchString = controlFlow.TargetJs.affects[control] | ||
const targetPath = controlFlow.TargetJs.src | ||
|
||
webkitItems.push({ matchString, targetPath, fileType: ConditionalControlFlowType.TargetJs }) | ||
} | ||
} | ||
} | ||
|
||
// Add patch keys into the webkitItems array | ||
for (const item in theme?.Patches) { | ||
const patch = theme.Patches[item] | ||
|
||
if (patch.TargetCss) { | ||
if (Array.isArray(patch.TargetCss)) { | ||
for (const target in patch.TargetCss) { | ||
webkitItems.push({ matchString: patch.MatchRegexString, targetPath: patch.TargetCss[target], fileType: ConditionalControlFlowType.TargetCss }) | ||
} | ||
} | ||
else { | ||
webkitItems.push({ matchString: patch.MatchRegexString, targetPath: patch.TargetCss, fileType: ConditionalControlFlowType.TargetCss }) | ||
} | ||
} | ||
|
||
if (patch.TargetJs) { | ||
if (Array.isArray(patch.TargetJs)) { | ||
for (const target in patch.TargetJs) { | ||
webkitItems.push({ matchString: patch.MatchRegexString, targetPath: patch.TargetJs[target], fileType: ConditionalControlFlowType.TargetJs }) | ||
} | ||
} | ||
else { | ||
webkitItems.push({ matchString: patch.MatchRegexString, targetPath: patch.TargetJs, fileType: ConditionalControlFlowType.TargetJs }) | ||
} | ||
} | ||
} | ||
|
||
// Filter out duplicates | ||
webkitItems = webkitItems.filter((item, index, self) => | ||
index === self.findIndex((t) => ( | ||
t.matchString === item.matchString && t.targetPath === item.targetPath | ||
)) | ||
) | ||
|
||
return webkitItems | ||
} | ||
|
||
const CreateWebkitScript = (themeData: ThemeItem) => { | ||
console.log(themeData) | ||
|
||
let script = ``; | ||
const webkitItems: WebkitItem[] = ParseMatchAndTarget(themeData?.data) | ||
|
||
for (const item in webkitItems) { | ||
const webkitItem = webkitItems[item] | ||
|
||
const cssPath = constructThemePath(themeData?.native, webkitItem.targetPath) | ||
const jsPath = ["https://s.ytimg.com/millennium-virtual", "skins", themeData?.native, webkitItem.targetPath].join('/') | ||
|
||
script += ` | ||
if (RegExp("${webkitItem.matchString}").test(window.location.href)) { | ||
console.log("Matched: ${webkitItem.matchString}, injecting ${webkitItem.fileType === ConditionalControlFlowType.TargetCss ? cssPath : jsPath}"); | ||
${ | ||
webkitItem.fileType === ConditionalControlFlowType.TargetCss | ||
? `document.head.appendChild(Object.assign(document.createElement('link'), { rel: 'stylesheet', href: '${cssPath}' }))` | ||
: `document.head.appendChild(Object.assign(document.createElement('script'), { src: '${jsPath}' }))` | ||
} | ||
}\n | ||
` | ||
} | ||
|
||
console.log(script) | ||
Millennium.callServerMethod("inject_webkit_shim", { shim_script: | ||
`(() => { ${script} })()` | ||
}) | ||
} | ||
|
||
export { CreateWebkitScript } |
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