Skip to content

Commit aabb06f

Browse files
committed
Chromeの仕様変更に対応
1 parent d58ef99 commit aabb06f

File tree

1 file changed

+45
-7
lines changed

1 file changed

+45
-7
lines changed

BrowserCookieImplementations/Chrome.cs

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,54 @@ public List<IBrowserProfile> GetProfiles()
2323
//恐らくChromeがインストールされていない
2424
return list;
2525
}
26-
var defaultDbFilePath = ChromeSettingsDirPath + _defaultProfileName + "\\" + _dbFilename;
27-
if (System.IO.File.Exists(defaultDbFilePath))
26+
//2021/12/12
27+
//仕様変更があった。
28+
//Cookiesファイルの配置が変わった
29+
//旧:"PROFILENAME"\Cookies
30+
//新:"PROFILENAME"\Network\Cookies
31+
//"Default"プロファイルは新しい配置になっていたけど、他のプロファイルはそのままだった。
32+
//新しいプロファイルを作成すると新しい配置になっていた。
33+
var defaultProfilePath = Path.Combine(ChromeSettingsDirPath, _defaultProfileName);
34+
var defaultDbFilePathOld = GetCookiesFileOldPath(defaultProfilePath, _dbFilename);
35+
var defaultDbFilePath = GetCookiesFilePath(defaultProfilePath, _dbFilename);
36+
if (System.IO.File.Exists(defaultDbFilePathOld))
2837
{
29-
list.Add(new ChromeProfile(Type, ChromeSettingsDirPath, _defaultProfileName));
38+
list.Add(new ChromeProfile(Type, ChromeSettingsDirPath, defaultDbFilePathOld, _defaultProfileName));
39+
}
40+
else if (System.IO.File.Exists(defaultDbFilePath))
41+
{
42+
list.Add(new ChromeProfile(Type, ChromeSettingsDirPath, defaultDbFilePath, _defaultProfileName));
3043
}
3144
var dirs = System.IO.Directory.GetDirectories(ChromeSettingsDirPath);
3245
foreach (var dir in dirs)
3346
{
3447
var dirName = System.IO.Path.GetFileName(dir);
35-
var testPath = dir + "\\" + _dbFilename;
36-
if (dirName.StartsWith("Profile", StringComparison.CurrentCultureIgnoreCase) && System.IO.File.Exists(testPath))
48+
if (!dirName.StartsWith("Profile", StringComparison.CurrentCultureIgnoreCase))
49+
{
50+
continue;
51+
}
52+
var profileCookiesFilePath = GetCookiesFilePath(dir, _dbFilename);
53+
var profileCookiesFilePathOld = GetCookiesFileOldPath(dir, _dbFilename);
54+
var profileName = dirName;
55+
if (File.Exists(profileCookiesFilePath))
3756
{
38-
var profileName = dirName;
39-
list.Add(new ChromeProfile(Type, ChromeSettingsDirPath, profileName));
57+
list.Add(new ChromeProfile(Type, ChromeSettingsDirPath, profileCookiesFilePath, profileName));
58+
}
59+
else if (File.Exists(profileCookiesFilePathOld))
60+
{
61+
list.Add(new ChromeProfile(Type, ChromeSettingsDirPath, profileCookiesFilePathOld, profileName));
4062
}
4163
}
4264
return list;
4365
}
66+
private static string GetCookiesFilePath(string profilePath, string cookieFileName)
67+
{
68+
return Path.Combine(profilePath, "Network", cookieFileName);
69+
}
70+
private static string GetCookiesFileOldPath(string profilePath, string cookieFileName)
71+
{
72+
return Path.Combine(profilePath, cookieFileName);
73+
}
4474
#endregion
4575
private readonly string _dbFilename = "Cookies";
4676
private readonly string _defaultProfileName = "Default";
@@ -73,13 +103,21 @@ public Cookie GetCookie(string domain, string name)
73103
#endregion
74104

75105
#region Constructors
106+
[Obsolete]
76107
public ChromeProfile(BrowserType type, string userDataDirPath, string profileName)
77108
{
78109
Path = userDataDirPath + "" + profileName + "\\Cookies";
79110
ProfileName = profileName;
80111
Type = type;
81112
_decryptor.LocalStatePath = userDataDirPath + "Local State";
82113
}
114+
public ChromeProfile(BrowserType type, string userDataDirPath, string cookiesPath, string profileName)
115+
{
116+
Path = cookiesPath;
117+
ProfileName = profileName;
118+
Type = type;
119+
_decryptor.LocalStatePath = userDataDirPath + "Local State";
120+
}
83121
#endregion
84122

85123
#region Methods

0 commit comments

Comments
 (0)