/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using log4net;
using Microsoft.Win32;
namespace GreenshotPlugin.Core {
///
/// Description of IEHelper.
///
public static class IEHelper {
private static readonly ILog Log = LogManager.GetLogger(typeof(IEHelper));
// Internet explorer Registry key
private const string IeKey = @"Software\Microsoft\Internet Explorer";
///
/// Get the current browser version
///
/// int with browser version
public static int IEVersion
{
get
{
var maxVer = 7;
using (var ieKey = Registry.LocalMachine.OpenSubKey(IeKey, false))
{
foreach (var value in new[] { "svcVersion", "svcUpdateVersion", "Version", "W2kVersion" })
{
var objVal = ieKey.GetValue(value, "0");
var strVal = Convert.ToString(objVal);
var iPos = strVal.IndexOf('.');
if (iPos > 0)
{
strVal = strVal.Substring(0, iPos);
}
if (int.TryParse(strVal, out var res))
{
maxVer = Math.Max(maxVer, res);
}
}
}
return maxVer;
}
}
///
/// Get the highest possible version for the embedded browser
///
/// true to ignore the doctype when loading a page
/// IE Feature
public static int GetEmbVersion(bool ignoreDoctype = true)
{
var ieVersion = IEVersion;
if (ieVersion > 9)
{
return ieVersion * 1000 + (ignoreDoctype ? 1 : 0);
}
if (ieVersion > 7)
{
return ieVersion * 1111;
}
return 7000;
}
///
/// Fix browser version to the highest possible
///
/// true to ignore the doctype when loading a page
public static void FixBrowserVersion(bool ignoreDoctype = true)
{
var applicationName = Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location);
FixBrowserVersion(applicationName, ignoreDoctype);
}
///
/// Fix the browser version for the specified application
///
/// Name of the process
/// true to ignore the doctype when loading a page
public static void FixBrowserVersion(string applicationName, bool ignoreDoctype = true)
{
FixBrowserVersion(applicationName, GetEmbVersion(ignoreDoctype));
}
///
/// Fix the browser version for the specified application
///
/// Name of the process
///
/// Version, see
/// Browser Emulation
///
public static void FixBrowserVersion(string applicationName, int ieVersion)
{
ModifyRegistry("HKEY_CURRENT_USER", applicationName + ".exe", ieVersion);
#if DEBUG
ModifyRegistry("HKEY_CURRENT_USER", applicationName + ".vshost.exe", ieVersion);
#endif
}
///
/// Make the change to the registry
///
/// HKEY_CURRENT_USER or something
/// Name of the executable
/// Version to use
private static void ModifyRegistry(string root, string applicationName, int ieFeatureVersion)
{
var regKey = root + @"\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION";
try
{
Registry.SetValue(regKey, applicationName, ieFeatureVersion);
}
catch (Exception ex)
{
// some config will hit access rights exceptions
// this is why we try with both LOCAL_MACHINE and CURRENT_USER
Log.Error(ex);
Log.ErrorFormat("couldn't modify the registry key {0}", regKey);
}
}
///
/// Find the DirectUI window for MSAA (Accessible)
///
/// The browser WindowDetails
/// WindowDetails for the DirectUI window
public static WindowDetails GetDirectUI(WindowDetails browserWindowDetails) {
if (browserWindowDetails == null) {
return null;
}
WindowDetails tmpWd = browserWindowDetails;
// Since IE 9 the TabBandClass is less deep!
if (IEVersion < 9) {
tmpWd = tmpWd.GetChild("CommandBarClass");
tmpWd = tmpWd?.GetChild("ReBarWindow32");
}
tmpWd = tmpWd?.GetChild("TabBandClass");
tmpWd = tmpWd?.GetChild("DirectUIHWND");
return tmpWd;
}
///
/// Return an IEnumerable with the currently opened IE urls
///
///
public static IEnumerable GetIEUrls() {
// Find the IE window
foreach (WindowDetails ieWindow in WindowDetails.GetAllWindows("IEFrame")) {
WindowDetails directUIWD = GetDirectUI(ieWindow);
if (directUIWD != null) {
Accessible ieAccessible = new Accessible(directUIWD.Handle);
foreach(string url in ieAccessible.IETabUrls)
{
yield return url;
}
}
}
}
}
}