-
Notifications
You must be signed in to change notification settings - Fork 1
/
Chrome_Selenium.py
87 lines (59 loc) · 2.76 KB
/
Chrome_Selenium.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import time
import os
import difflib
import utils
from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
EXTENSION_ID = "chphlpgkkbolifaimnlloiipkdnihall"
USER_PROFILE = os.environ['USERPROFILE']
LOCAL_DESTINATION_FILE_PATH = os.path.join(
USER_PROFILE, "Documents", "OneTab-Backup.txt")
CHROME_DIR = os.path.join(USER_PROFILE, 'AppData', 'Local', 'Google', 'Chrome')
CHROME_USER_DATA_DIR = os.path.join(CHROME_DIR, 'User Data')
DEFAULT_CHROME_USER_DATA_DIR = os.path.join(
CHROME_DIR, "User Data", "Default")
TEMP_DIR = os.path.join(CHROME_DIR, 'temp',)
TEMP_CHROME_USER_DATA = os.path.join(TEMP_DIR, "Default")
def check_need_to_update(filepath, latestData):
if os.path.exists(filepath):
exisitingFile = open(filepath, 'r', encoding="utf-8")
diffResult = difflib.Differ().compare(exisitingFile.read(), latestData)
# List Comprehension
return [x for x in diffResult if x[0] in ('+', '-')]
else:
return True
def create_or_update_backup_file(latestData):
if check_need_to_update(LOCAL_DESTINATION_FILE_PATH, latestData):
file = open(LOCAL_DESTINATION_FILE_PATH, 'w', encoding="utf-8")
file.write(latestData)
file.close()
print("Backup Created/Updated")
else:
print("No new changes")
utils.remove_directory_if_exists(TEMP_DIR)
TEMP_LOCAL_EXTENSION_SETTINGS_EXTENSION_DIR = os.path.join(
TEMP_CHROME_USER_DATA, "Local Extension Settings", EXTENSION_ID)
chrome_options = ChromeOptions()
chrome_options.add_argument(
"user-data-dir={}".format(TEMP_DIR))
chrome_options.add_extension('./onetab.crx')
#==================== Generate a default blank profile ==============================
driver = webdriver.Chrome('chromedriver.exe', options=chrome_options)
time.sleep(1) # Let the user actually see something!
driver.close()
#===================== Copy OneTab Data =============================================
print('Start Copying OneTab Data')
utils.copy_all_files_in_directory(os.path.join(CHROME_USER_DATA_DIR, "Default", "Local Extension Settings",
EXTENSION_ID), TEMP_LOCAL_EXTENSION_SETTINGS_EXTENSION_DIR, ["LOCK"])
print('Complete Copied')
#===================== Backup OneTab Data =============================================
driver = webdriver.Chrome('chromedriver.exe', options=chrome_options)
driver.get("chrome-extension://chphlpgkkbolifaimnlloiipkdnihall/import-export.html")
time.sleep(1) # Let the user actually see something!
contentArea = driver.find_element_by_id("contentAreaDiv")
export_box = driver.find_elements_by_tag_name("textarea")[1]
text = export_box.get_attribute("value")
create_or_update_backup_file(text)
driver.close()
driver.quit()
utils.remove_directory_if_exists(TEMP_DIR)