feature: script.js,支持相对地址。
parent
b90263ab73
commit
0b4f3d1397
|
@ -5,14 +5,10 @@ function getScript (key, script) {
|
||||||
const scriptUrl = contextPath + key
|
const scriptUrl = contextPath + key
|
||||||
|
|
||||||
const hash = CryptoJs.SHA256(script).toString(CryptoJs.enc.Base64)
|
const hash = CryptoJs.SHA256(script).toString(CryptoJs.enc.Base64)
|
||||||
return `
|
return `<script crossorigin="anonymous" defer="defer" type="application/javascript" src="${scriptUrl}" integrity="sha256-${hash}"></script>`
|
||||||
<script src="${scriptUrl}" type="application/javascript" crossorigin="anonymous" defer integrity="sha256-${hash}"></script>
|
|
||||||
`
|
|
||||||
}
|
}
|
||||||
function getScriptByAbsoluteUrl (scriptUrl) {
|
function getScriptByUrlOrPath (scriptUrlOrPath) {
|
||||||
return `
|
return `<script crossorigin="anonymous" defer="defer" type="application/javascript" src="${scriptUrlOrPath}"></script>`
|
||||||
<script src="${scriptUrl}" type="application/javascript" crossorigin="anonymous" defer></script>
|
|
||||||
`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
@ -20,32 +16,38 @@ module.exports = {
|
||||||
priority: 202,
|
priority: 202,
|
||||||
responseIntercept (context, interceptOpt, req, res, proxyReq, proxyRes, ssl, next) {
|
responseIntercept (context, interceptOpt, req, res, proxyReq, proxyRes, ssl, next) {
|
||||||
const { rOptions, log, setting } = context
|
const { rOptions, log, setting } = context
|
||||||
|
|
||||||
|
// github特殊处理
|
||||||
|
if (rOptions.hostname === 'github.com' && rOptions.headers['turbo-frame'] === 'repo-content-turbo-frame') {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
let keys = interceptOpt.script
|
let keys = interceptOpt.script
|
||||||
if (typeof keys === 'string') {
|
if (typeof keys === 'string') {
|
||||||
keys = [keys]
|
keys = [keys]
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const scripts = monkey.get(setting.script.dirAbsolutePath)
|
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) {
|
for (const key of keys) {
|
||||||
let scriptTag
|
let scriptTag
|
||||||
|
|
||||||
if (key.indexOf('http:') === 0 || key.indexOf('https:') === 0) {
|
if (key.indexOf('/') >= 0) {
|
||||||
scriptTag = getScriptByAbsoluteUrl(key)
|
scriptTag = getScriptByUrlOrPath(key) // 1.绝对地址或相对地址(注意:当目标站点限制跨域脚本资源的情况下,可以使用相对地址,再结合proxy.js进行代理,可规避掉跨域限制问题。)
|
||||||
} else {
|
} else {
|
||||||
const script = scripts[key]
|
const script = scripts[key]
|
||||||
if (script == null) {
|
if (script == null) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
scriptTag = getScript(key, script.script)
|
scriptTag = getScript(key, script.script) // 2.DS内置脚本
|
||||||
}
|
}
|
||||||
|
|
||||||
tags += '\r\n' + scriptTag
|
tags += '\r\n\t' + scriptTag
|
||||||
}
|
}
|
||||||
res.setHeader('DS-Script-Interceptor', 'true')
|
res.setHeader('DS-Script-Interceptor', 'true')
|
||||||
log.info('script response intercept: insert script', rOptions.hostname, rOptions.path, ', head:', tags)
|
log.info('script response intercept: insert script', rOptions.hostname, rOptions.path, ', head:', tags)
|
||||||
return {
|
return {
|
||||||
head: tags
|
head: tags + '\r\n'
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
res.setHeader('DS-Script-Interceptor', 'error')
|
res.setHeader('DS-Script-Interceptor', 'error')
|
||||||
|
|
Loading…
Reference in New Issue