简体   繁体   English

C#如何从Chrome获取当前网址(多种语言设置)

[英]C# How to get Current URL from Chrome (Multiple language setting)

I am trying to get current URL from the Chrome browser. 我正在尝试从Chrome浏览器获取当前URL。 My code is working when my browser is in English language. 当我的浏览器为英语时,我的代码可以正常工作。 But when someone has a different language it doesn't work due to comparison. 但是,当某人使用另一种语言时,由于比较而无法正常工作。

AutomationElement elm = AutomationElement.FromHandle(p.MainWindowHandle);
AutomationElement elmUrlBar = null;

// elmUrlBar = elm.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty,   "Adresse und Suchleiste"));
// if (elmUrlBar != null)
elmUrlBar = elm.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));

if (elmUrlBar != null)
{
    AutomationPattern[] patterns = elmUrlBar.GetSupportedPatterns();
    if (patterns.Length > 0)
    {
        ValuePattern val = (ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0]);
        objSchemeDetail.ProcessName = val.Current.Value;
    }
}

I changed "Address and search bar" into German but it still doesn't work. 我将“地址和搜索栏”更改为德语,但仍然无法正常工作。 What is it's replacement? 什么是替代品?

Chrome is a build of Chromium provided by Google. Chrome是Google提供的Chromium版本。 The chromium source code is at chromium.googlesource.com . 铬源代码位于chromium.googlesource.com Searching for "Address and search bar" I found generated_resources_en-GB.xtb showing the english text with an ID, for example: 在搜索“地址和搜索栏”时,我发现了generate_resources_en-GB.xtb,其中显示了带有ID的英文文本,例如:

<translation id="2824775600643448204">Address and search bar</translation>

When I search in the German version ( generated_resources_de.xtb ) for the same ID, I find 当我在德语版本( generate_resources_de.xtb )中搜索相同的ID时,我发现

<translation id="2824775600643448204">Adress- und Suchleiste</translation>

So it looks like you have to check this file for each language you want to support. 因此,您似乎必须在此文件中检查要支持的每种语言。 In chromium version 48 and newer, this text seems to have moved to a different location. 在铬版本48及更高版本中,此文本似乎已移至其他位置。

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

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