javascript - 打开 IE 并注入(inject)变量值(例如 ScriptEngineMinorVersion)

标签 javascript html vbscript internet-explorer-9

我想制作一个打开脚本并注入(inject)值的脚本。

我有基本部分。 (示例)

Set WshShell = WScript.CreateObject("WScript.Shell") 
Return = WshShell.Run("iexplore.exe google.com", 1) 

但我还想覆盖一些变量值。我正在使用的这个页面有一些 JavaScript 验证,我想用我自己的值覆盖它们。

示例:ScriptEngineMinorVersion

这是我想要绕过的主要功能,较新版本的 IE 未通过此测试,因为检查 ScriptEngineMinorVersion 的位写得不好,并且 vendor 不再支持该产品。

function IsBrowserVersionAcceptable()
{
    // Browser detection
    var browser = GetCurrentBrowserVersion();

        // See if the browser is allowed/tested
    if (browser.IsTested())
    {
        AddTestResultRow('BrowserVersion',
                         'OK',
                         'Browser type/version', // TRANS: msgBrowserTypeVersion
                         browser.GetShortDescription(),
                         '');
    }
    else if (browser.IsAllowed())
    {
        AddTestResultRow('BrowserVersion',
                         'Warning',
                         'Browser type/version', // TRANS: msgBrowserTypeVersion
                         browser.GetShortDescription(),
                         // TRANS: msgBrowserTypeVersionNotTested
                         'Using this webpage with your browser is not tested. Webpage client will start, but may not be 100% functional. No warranty is given from this point on.');
        AddWarning(new CWarning('browserversion', browser.GetID()));
    }
    else
    {
        // TRANS: use original message...
        // alert(MsgFormat(localedb.FindString('msgBrowserTooOld').strText, GetTestedBrowserVersions(), browserversion.GetDescription()));
        AddTestResultRow('BrowserVersion',
                         'Error',
                         'Browser type/version', // TRANS: msgBrowserTypeVersion
                         browser.GetShortDescription(),
                         // TRANS: msgBrowserTypeVersionNotAllowed
                         'Webpage does not support the use of the browser you are using at this moment.<br>' +
                         '<small>(User Agent = ' + new String(navigator.userAgent) + ')</small>');
        // Get out, maybe tell the user we will not go on...
        // TRANS: msgErrorCannotGoOn
        ShowStopMessage('There is an error, Webpage cannot go on.');
        return false;
    }

    // Check Script engine version
    if("undefined" == typeof ScriptEngine)
    {
        AddTestResultRow('ScriptEngine',
                         'Error',
                         'Script Engine', // TRANS: msgScriptEngine
                         '-',
                         // TRANS: msgScriptEngineNotAvailable
                         'You do not have a MS Script Engine. This means your browser is not IE or you do not have the Script Engine installed on your system. The Script Engine is needed for this webpage to function correctly.');
        // TRANS: msgErrorCannotGoOn
        ShowStopMessage('There is an error, webpage cannot go on.');
        return false;
    }
    else
    {
        strScriptEngineVer = ScriptEngineMajorVersion() + '.' + ScriptEngineMinorVersion();
        if (ScriptEngineMajorVersion() == 9.0 ||
            ScriptEngineMinorVersion() == 9.0) 
        {
            AddTestResultRow('ScriptEngine',
                             'OK',
                             'Script Engine', // TRANS: msgScriptEngine
                             strScriptEngineVer,
                             '');                   
        }
        else
        {
        if (ScriptEngineMajorVersion() < 5 ||
            ScriptEngineMinorVersion() < 5)
        {
            AddTestResultRow('ScriptEngine',
                             'Error',
                             'Script Engine', // TRANS: msgScriptEngine
                             strScriptEngineVer,
                             // TRANS: msgScriptEngineWrongVersion
                             'Your version of MS Script Engine is not sufficient, webpage will not function correctly.');
            // TRANS: msgErrorCannotGoOn
            ShowStopMessage('There is an error, webpage cannot go on.');
            return false;
        }
        else
        {
            AddTestResultRow('ScriptEngine',
                             'OK',
                             'Script Engine', // TRANS: msgScriptEngine
                             strScriptEngineVer,
                             '');
        }
}
    }
    return true;
}

最佳答案

您需要覆盖 builtin function通过在 VBScript 中重新实现它:

Function ScriptEngineMinorVersion
  ScriptEngineMinorVersion = 42
End Function

或 JavaScript:

function ScriptEngineMinorVersion() {
  return 42;
}

但是,您启动 Internet Explorer 的方式并不能让您访问该实例。您可以附加到已经运行的实例:

Set sh = CreateObject("Shell.Application")
For Each wnd In sh.Windows
  If InStr(1, wnd.FullName, "iexplore.exe", vbTextCompare) > 0 Then
    Set ie = wnd
    Exit For
  End If
Next

或者(更好)像这样生成实例:

Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.Navigate "http://www.google.com"

While ie.ReadyState <> 4 : WScript.Sleep 100 : Wend

但即便如此,我也不确定函数重定义是否真的有帮助,因为网页的脚本代码运行在不同的引擎中,所以你需要将函数注入(inject)到网页中,然后你需要在浏览器渲染之前执行此操作。注入(inject)可以这样完成:

Set script = ie.document.createElement("script")
script.type = "text/vbscript"
script.text = "Function ScriptEngineMinorVersion : " & _
              "ScriptEngineMinorVersion = 42 : " & _
              "End Function"
ie.document.head.appendChild(script)

但我不知道有什么方法可以触发页面内容的重新评估(以便重新运行页面中的代码)而不实际重新加载它(这将撤消注入(inject))。

关于javascript - 打开 IE 并注入(inject)变量值(例如 ScriptEngineMinorVersion),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31364103/

相关文章:

javascript - 如何在没有 Prop 的情况下从 react 中获取当前路线

javascript - onclick pop 在每个页面重新加载后起作用

javascript - 从本地主机加载时图像闪烁

javascript - 获取点击的 ul 的 li 列表

javascript - 选中复选框时如何刷新页面的一部分?

html - 绝对div可以在响应式设计中使用吗

html - SVG 标记在 Internet Explorer 中损坏?

javascript - 如何转换 VBS On error resume Next to javascript

error-handling - VBscript : Verifying that a file has been completely copied/error handling

google-chrome - Vb 脚本不适用于 Chrome 或 Firefox - 仅适用于 Internet Explorer