Programmatically access the Google Chrome Home or Start page

前端 未结 3 1774
终归单人心
终归单人心 2020-12-01 06:51

Where does Chrome save the Home or Start page URL? I want to access it programmatically using C#.

相关标签:
3条回答
  • 2020-12-01 07:08

    On Windows 7 (and I guess Vista) with a default install it's stored in the file:

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

    On Windows 2003 (and XP):

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

    The property name to look for is: homepage.

    0 讨论(0)
  • 2020-12-01 07:08

    A little part of the Preferences file you need.

    },
          "homepage": "http://www.google.com/",
          "homepage_is_newtabpage": true,
          "pinned_tabs": [ {
    
    0 讨论(0)
  • 2020-12-01 07:21

    Default locations are:

    Windows XP

    Google Chrome: 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

    Google Chrome: C:\Users\<username>\AppData\Local\Google\Chrome\User Data\Default
    Chromium: C:\Users\<username>\AppData\Local\Chromium\User Data\Default

    Mac OS X

    Google Chrome: ~/Library/Application Support/Google/Chrome/Default
    Chromium: ~/Library/Application Support/Chromium/Default

    Linux

    Google Chrome: ~/.config/google-chrome/Default
    Chromium: ~/.config/chromium/Default

    Source: Google Chromium user data directory default locations. ( link )

    In amount of time I spent on writing this, this was the shortest and most robust example I could think of (I completely ignored the fact, that user could use different location then default). Must say, it was bit trickier, then I thought.

    In this example, I try using the default location directory, and finding the preference file where the "Home" is stored. It is stored in JSon format, so I deserialize the data that I am interested, and print it out.

    Win 7 Example:

    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);
            }
        }
    }
    

    Example output for Me:

    chrome://newtab
    True
    

    Hope I get at least 1 up vote :P

    0 讨论(0)
提交回复
热议问题