feature: `script` 拦截器,支持绝对地址和相对地址配置,该拦截器不再受限于DS的发版了。 (#294)

pull/295/head
王良 2024-04-14 01:29:54 +08:00 committed by GitHub
parent 698029c001
commit 5d61e4dfe1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 12 deletions

View File

@ -132,7 +132,7 @@
if (GM_getValue('menu_rawDownLink')) menu_rawFast_ID = GM_registerMenuCommand(`${['0⃣','1⃣','2⃣','3⃣','4⃣','5⃣','6⃣','7⃣','8⃣','9⃣','🔟'][menu_rawFast]} [ ${raw_url[menu_rawFast][1]} ] 加速源 (☁) - 点击切换`, menu_toggle_raw_fast);
menu_rawDownLink_ID = GM_registerMenuCommand(`${GM_getValue('menu_rawDownLink')?'✅':'❌'} 项目列表单文件快捷下载 (☁)`, function(){if (GM_getValue('menu_rawDownLink') == true) {GM_setValue('menu_rawDownLink', false); GM_notification({text: `已关闭 [项目列表单文件快捷下载 (☁)] 功能\n点击刷新网页后生效`, timeout: 3500, onclick: function(){location.reload();}});} else {GM_setValue('menu_rawDownLink', true); GM_notification({text: `已开启 [项目列表单文件快捷下载 (☁)] 功能\n点击刷新网页后生效`, timeout: 3500, onclick: function(){location.reload();}});}registerMenuCommand();});
menu_gitClone_ID = GM_registerMenuCommand(`${GM_getValue('menu_gitClone')?'✅':'❌'} 添加 git clone 命令`, function(){if (GM_getValue('menu_gitClone') == true) {GM_setValue('menu_gitClone', false); GM_notification({text: `已关闭 [添加 git clone 命令] 功能\n点击刷新网页后生效`, timeout: 3500, onclick: function(){location.reload();}});} else {GM_setValue('menu_gitClone', true); GM_notification({text: `已开启 [添加 git clone 命令] 功能\n点击刷新网页后生效`, timeout: 3500, onclick: function(){location.reload();}});}registerMenuCommand();});
menu_feedBack_ID = GM_registerMenuCommand('💬 反馈 & 建议 [Github]', function () {window.GM_openInTab('https://github.com/XIU2/UserScript', {active: true,insert: true,setParent: true});window.GM_openInTab('https://greasyfork.org/zh-CN/scripts/412245/feedback', {active: true,insert: true,setParent: true});});
menu_feedBack_ID = GM_registerMenuCommand('💬 反馈 & 建议 [Github]', function () {GM_openInTab('https://github.com/XIU2/UserScript', {active: true,insert: true,setParent: true});GM_openInTab('https://greasyfork.org/zh-CN/scripts/412245/feedback', {active: true,insert: true,setParent: true});});
}
// 切换加速源

View File

@ -5,11 +5,10 @@ function getScript (key, script) {
const scriptUrl = contextPath + key
const hash = CryptoJs.SHA256(script).toString(CryptoJs.enc.Base64)
return `
<script crossorigin="anonymous" defer="defer" type="application/javascript"
integrity="sha256-${hash}"
src="${scriptUrl}"></script>
`
return `<script crossorigin="anonymous" defer="defer" type="application/javascript" src="${scriptUrl}" integrity="sha256-${hash}"></script>`
}
function getScriptByUrlOrPath (scriptUrlOrPath) {
return `<script crossorigin="anonymous" defer="defer" type="application/javascript" src="${scriptUrlOrPath}"></script>`
}
module.exports = {
@ -17,25 +16,42 @@ module.exports = {
priority: 202,
responseIntercept (context, interceptOpt, req, res, proxyReq, proxyRes, ssl, next) {
const { rOptions, log, setting } = context
// github特殊处理
if (rOptions.hostname === 'github.com' && rOptions.headers['turbo-frame'] === 'repo-content-turbo-frame') {
return
}
let keys = interceptOpt.script
if (typeof keys === 'string') {
keys = [keys]
}
try {
const scripts = monkey.get(setting.script.dirAbsolutePath)
let tags = getScript('global', scripts.global.script)
let tags = '\r\n\t' + getScript('global', scripts.global.script)
for (const key of keys) {
const script = scripts[key]
if (script == null) {
if (key === 'global') {
continue
}
const scriptTag = getScript(key, script.script)
tags += '\r\n' + scriptTag
let scriptTag
if (key.indexOf('/') >= 0) {
scriptTag = getScriptByUrlOrPath(key) // 1.绝对地址或相对地址注意当目标站点限制跨域脚本时可使用相对地址再结合proxy拦截器进行代理可规避掉限制跨域脚本问题。
} else {
const script = scripts[key]
if (script == null) {
continue
}
scriptTag = getScript(key, script.script) // 2.DS内置脚本
}
tags += '\r\n\t' + scriptTag
}
res.setHeader('DS-Script-Interceptor', 'true')
log.info('script response intercept: insert script', rOptions.hostname, rOptions.path, ', head:', tags)
return {
head: tags
head: tags + '\r\n'
}
} catch (err) {
res.setHeader('DS-Script-Interceptor', 'error')