简体   繁体   English

如何使脚本标签适应 Angular?

[英]How to adapt script tag to Angular?

On my old website, I have had some problems with some bad guys, who have tried to clone my site.在我的旧网站上,我遇到了一些坏人的问题,他们试图克隆我的网站。 Now I try to pass the application to Angular7.现在我尝试将应用程序传递给 Angular7。

One of the measures that I have implemented in the old page, to end the attempts to clone, has been using a .js code to check the hostname from the header.为了结束克隆尝试,我在旧页面中实施的措施之一是使用.js代码从标题中检查主机名。

<script type="text/javascript">
if(!['mysite.com','testsite.com'].includes(window.location.hostname)){
            window.location.href = 'https://google.com'; 
}
</script>

Can someone tell me how I can convert (rewrite, adapt) this Javascript code so that it can be used in Angular?有人能告诉我如何转换(重写、改编)这段 Javascript 代码,以便它可以在 Angular 中使用吗?

I want to use this code (if possible) in the source code, so it can be compiled after the run - npm run build - so that it stays integrated in main.js of my website.我想在源代码中使用这个代码(如果可能的话),所以它可以在运行后编译 - npm run build - 以便它保持集成在我网站的main.js中。

in your index.html在你的 index.html

add this code添加此代码

example例子

src/app/index.html源代码/应用程序/index.html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Stackoverflow</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <app-root></app-root>

  <script type="text/javascript">
  if(!['mysite.com','testsite.com'].includes(window.location.hostname)){
            window.location.href = 'https://google.com'; 
  }
</script>
</body>
</html>

ok 👍好的👍

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM