Skip to content

Commit

Permalink
feature: script 拦截器,支持绝对地址和相对地址配置,该拦截器不再受限于DS的发版了。 (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangliang181230 committed Apr 13, 2024
1 parent 698029c commit 5d61e4d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/gui/extra/scripts/github.script
Original file line number Diff line number Diff line change
Expand Up @@ -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});});
}

// 切换加速源
Expand Down
38 changes: 27 additions & 11 deletions packages/mitmproxy/src/lib/interceptor/impl/res/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,53 @@ 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 = {
name: 'script',
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')
Expand Down

0 comments on commit 5d61e4d

Please sign in to comment.