gpt4 book ai didi

cdn - 是否可以通过 Akamai 将脚本注入(inject) HTML 响应?

转载 作者:行者123 更新时间:2023-12-05 04:25:00 25 4
gpt4 key购买 nike

这是一个边缘用例,但我想将一些 JavaScript 注入(inject)到页面中,而无需在后端进行代码更改。所以我想知道我是否可以为 Akamai 的特定路由设置规则,该规则会注入(inject)或添加 <script>标记到 HTML 正文中。我可以托管外部 JavaScript 文件,但我只是不想在 HTML 响应的源头进行更改。

最佳答案

在 Akamai 中,可以使用 EdgeWorkers 修改 HTML 内容。例如,find-replace-stream example可以修改为注入(inject) <script>标签末尾的标签。

export function responseProvider(request) {
// Get text to be searched for and new replacement text from Property Manager variables in the request object.
const tosearchfor = '</head>';
const newtext = request.getVariable('<script>......</script></head>');
// Must have a number larger than 0 to allow and limit replacements counts
const howManyReplacements = 1;

return httpRequest(`${request.scheme}://${request.host}${request.url}`).then(response => {
return createResponse(
response.status,
getSafeResponseHeaders(response.getHeaders()),
response.body
.pipeThrough(new TextDecoderStream())
.pipeThrough(new FindAndReplaceStream(tosearchfor, newtext, howManyReplacements))
.pipeThrough(new TextEncoderStream())
);
});
}

关于cdn - 是否可以通过 Akamai 将脚本注入(inject) HTML 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73386230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com