通过 Akamai 是否可能向 HTML 响应注入脚本?

4
这是一个比较特殊的情况,我想在不对后端代码进行修改的情况下向页面注入一些JavaScript。因此,我想知道是否可以在Akamai的特定路由上设置规则,将 <script> 标签注入或添加到HTML主体中。我可以托管外部JavaScript文件,但我不想对HTML响应源做出更改。
2个回答

2
在Akamai中,可以通过使用EdgeWorkers来修改HTML内容。例如,可以修改find-replace-stream示例,在结束标签中注入<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())
    );
  });
}


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接