【问题标题】:Programmatically access the Google Chrome Home or Start page以编程方式访问 Google Chrome 主页或开始页面
【发布时间】:2011-09-14 00:11:12
【问题描述】:

Chrome 将主页或起始页 URL 保存在哪里?我想使用 C# 以编程方式访问它。

【问题讨论】:

    标签: c# google-chrome


    【解决方案1】:

    默认位置是:

    Windows XP

    谷歌浏览器: C:\Documents and Settings\<username>\Local Settings\Application Data\Google\Chrome\User Data\Default
    Chromium: C:\Documents and Settings\<username>\Local Settings\Application Data\Chromium\User Data\Default

    Vista / 7

    谷歌浏览器: C:\Users\<username>\AppData\Local\Google\Chrome\User Data\Default
    Chromium: C:\Users\<username>\AppData\Local\Chromium\User Data\Default

    Mac OS X

    谷歌浏览器:~/Library/Application Support/Google/Chrome/Default
    Chromium:~/Library/Application Support/Chromium/Default

    Linux

    谷歌浏览器: ~/.config/google-chrome/Default
    Chromium: ~/.config/chromium/Default

    来源:Google Chromium 用户数据目录默认位置。 (link)

    在我写这篇文章所花费的时间中,这是我能想到的最短且最强大的示例(我完全忽略了这样一个事实,即用户可以使用与默认位置不同的位置)。必须说,这有点棘手,然后我想。

    在此示例中,我尝试使用默认位置目录,并找到存储“Home”的首选项文件。它以JSon格式存储,因此我将我感兴趣的数据反序列化并打印出来。

    Win 7 示例:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    //References -> Add Reference -> "System.Runtime.Serialization" Add
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Json;
    
    namespace test {
        class Program {
            [DataContract]
            public class Mdata {
                [DataMember(Name = "homepage")] 
                public String homepage { get; private set; }
                [DataMember(Name = "homepage_is_newtabpage")]
                public Boolean isNewTab { get; private set; }
                public Mdata() { }
                public Mdata(String data) {
                    homepage = data;
                }
            }
    
            public static Mdata FindData(String json) {
                Mdata deserializedData = new Mdata();
                MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
                DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedData.GetType());
                deserializedData = ser.ReadObject(ms) as Mdata;
                ms.Close();
                return deserializedData;
            }
    
            static void Main(string[] args) {
                const int LikeWin7 = 6;
                OperatingSystem osInfo = Environment.OSVersion;
                DirectoryInfo strDirectory;
                String path=null, file=null, data;
    
                if (osInfo.Platform.Equals(System.PlatformID.Win32NT))
                    if (osInfo.Version.Major == LikeWin7)
                        path = Environment.GetEnvironmentVariable("LocalAppData") +
                            @"\Google\Chrome\User Data\Default";
                if (path == null || path.Length == 0)
                    throw new ArgumentNullException("Fail. Bad OS.");
                if (!(strDirectory = new DirectoryInfo(path)).Exists)
                    throw new DirectoryNotFoundException("Fail. The directory was not fund");
                if (!new FileInfo(file = Directory.GetFiles(strDirectory.FullName, "Preferences*")[0]).Exists)
                    throw new FileNotFoundException("Fail. The file was not found.", file);
    
                Mdata info = FindData(data = System.IO.File.ReadAllText(file));
                Console.WriteLine(info.homepage);
                Console.WriteLine(info.isNewTab);
            }
        }
    }
    

    我的示例输出:

    chrome://newtab
    True
    

    希望我至少得到 1 票:P

    【讨论】:

    • 我尝试手动更改首选项文件。但是,当我更改并保存首选项文件时,该文件会重新加载以前的版本。我认为,如果您想要更改正常工作,则必须关闭 chrome。
    • 这个Get成功了,但是如何设置呢?
    【解决方案2】:

    在默认安装的 Windows 7(我猜是 Vista)上,它存储在文件中:

    %USERPROFILE%\AppData\Local\Google\User Data\Default\Preferences

    在 Windows 2003(和 XP)上:

    %USERPROFILE%\Local Settings\Application Data\Google\Chrome\User Data\Default\Preferences

    要查找的属性名称是:homepage

    【讨论】:

      【解决方案3】:

      您需要的 Preferences 文件的一小部分。

      },
            "homepage": "http://www.google.com/",
            "homepage_is_newtabpage": true,
            "pinned_tabs": [ {
      

      【讨论】:

        猜你喜欢
        • 2014-05-10
        • 1970-01-01
        • 1970-01-01
        • 2021-07-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多