Rewrite browser history, dynamic sorting by popularity

master
Mingde (Matthew) Zeng 6 years ago
parent ae284a4287
commit 40c3a25823
  1. 93
      core/browser.py
  2. 10
      eaf.el

@ -343,6 +343,12 @@ class BrowserCookieStorage:
open(self.cookie_file, 'w').close() open(self.cookie_file, 'w').close()
class HistoryPage():
def __init__(self, title, url, hit):
self.title = title
self.url = url
self.hit = float(hit)
class BrowserBuffer(Buffer): class BrowserBuffer(Buffer):
close_page = QtCore.pyqtSignal(str) close_page = QtCore.pyqtSignal(str)
@ -354,10 +360,26 @@ class BrowserBuffer(Buffer):
self.add_widget(BrowserView(config_dir)) self.add_widget(BrowserView(config_dir))
self.config_dir = config_dir self.config_dir = config_dir
self.history_log_file_path = os.path.join(self.config_dir, "browser", "history", "log.txt")
self.history_url_pattern = re.compile("(.*)\s((https?|file):[^\s]+)$") self.history_list = []
self.short_url_pattern = re.compile("^(https?|file)://(.+)") if self.emacs_var_dict["eaf-browser-remember-history"] == "true":
self.history_close_file_path = os.path.join(self.config_dir, "browser", "history", "close.txt") self.history_log_file_path = os.path.join(self.config_dir, "browser", "history", "log.txt")
self.history_pattern = re.compile("^(.+)ᛝ(.+)ᛡ(.+)$")
self.noprefix_url_pattern = re.compile("^(https?|file)://(.+)")
self.nopostfix_url_pattern = re.compile("^[^#\?]*")
self.history_close_file_path = os.path.join(self.config_dir, "browser", "history", "close.txt")
touch(self.history_log_file_path)
with open(self.history_log_file_path, "r") as f:
raw_list = f.readlines()
for raw_his in raw_list:
his_line = re.match(self.history_pattern, raw_his)
if his_line is None: # Obsolete Old history format
old_his = re.match("(.*)\s((https?|file):[^\s]+)$", raw_his)
if old_his is not None:
self.history_list.append(HistoryPage(old_his.group(1), old_his.group(2), 1))
else:
self.history_list.append(HistoryPage(his_line.group(1), his_line.group(2), his_line.group(3)))
# Set User Agent with Firefox's one to make EAF browser can login in Google account. # Set User Agent with Firefox's one to make EAF browser can login in Google account.
self.profile = QWebEngineProfile(self.buffer_widget) self.profile = QWebEngineProfile(self.buffer_widget)
@ -509,37 +531,38 @@ class BrowserBuffer(Buffer):
def record_history(self, new_title): def record_history(self, new_title):
new_url = self.buffer_widget.filter_url(self.buffer_widget.url().toString()) new_url = self.buffer_widget.filter_url(self.buffer_widget.url().toString())
if self.arguments != "temp_html_file" and new_title != "about:blank" and new_url != "about:blank" and \ if self.emacs_var_dict["eaf-browser-remember-history"] == "true" and self.buffer_widget.filter_title(new_title) != "" and \
self.emacs_var_dict["eaf-browser-remember-history"] == "true": self.arguments != "temp_html_file" and new_title != "about:blank" and new_url != "about:blank":
if self.buffer_widget.filter_title(new_title) != "": # Throw traceback info if algorithm has bug and protection of historical record is not erased.
touch(self.history_log_file_path) try:
with open(self.history_log_file_path, "r") as f: noprefix_new_url_match = re.match(self.noprefix_url_pattern, new_url)
lines = f.readlines() if noprefix_new_url_match is not None:
found = False
# Throw traceback info if algorithm has bug and protection of historical record is not erased. for history in self.history_list:
try: noprefix_url_match = re.match(self.noprefix_url_pattern, history.url)
new_lines = [] if (noprefix_url_match is not None):
for line in lines: noprefix_url = noprefix_url_match.group(2)
line_match = re.match(self.history_url_pattern, line) noprefix_new_url = noprefix_new_url_match.group(2)
if line_match != None: nopostfix_new_url_match = re.match(self.nopostfix_url_pattern, noprefix_new_url)
title = line_match.group(1) if(noprefix_url == noprefix_new_url): # found unique url
url = line_match.group(2) history.title = new_title
else: history.url = new_url
title = "" history.hit += 0.5
url = line found = True
elif (nopostfix_new_url_match is not None and noprefix_url == nopostfix_new_url_match.group()):
short_new_url = re.match(self.short_url_pattern, new_url) # also increment parent
short_url = re.match(self.short_url_pattern, url) history.hit += 0.25
if (short_new_url != None and short_url != None and short_url.group(2) != short_new_url.group(2)):
new_lines.append(line) if not found:
self.history_list.append(HistoryPage(new_title, new_url, 1))
new_lines.append("{0} {1}\n".format(new_title, new_url))
self.history_list.sort(key = lambda x: x.hit, reverse = True)
with open(self.history_log_file_path, "w") as f:
f.writelines(new_lines) with open(self.history_log_file_path, "w") as f:
except Exception: f.writelines(map(lambda history: history.title + "" + history.url + "" + str(history.hit) + "\n", self.history_list))
import traceback except Exception:
self.message_to_emacs.emit("Error in record_history: " + str(traceback.print_exc())) import traceback
self.message_to_emacs.emit("Error in record_history: " + str(traceback.print_exc()))
def adjust_dark_mode(self): def adjust_dark_mode(self):
try: try:

@ -1264,10 +1264,14 @@ This function works best if paired with a fuzzy search package."
(concat eaf-config-location (concat eaf-config-location
(file-name-as-directory "browser") (file-name-as-directory "browser")
(file-name-as-directory "history") (file-name-as-directory "history")
"log.txt"))) "log.txt"))
(history-pattern "^\\(.+\\)ᛝ\\(.+\\)ᛡ\\(.+\\)$"))
(if (file-exists-p browser-history-file-path) (if (file-exists-p browser-history-file-path)
(let* ((history-list (reverse (with-temp-buffer (insert-file-contents browser-history-file-path) (let* ((history-list (mapcar
(split-string (buffer-string) "\n" t)))) (lambda (h) (when (string-match history-pattern h)
(format "[%s] ⇰ %s" (match-string 1 h) (match-string 2 h))))
(with-temp-buffer (insert-file-contents browser-history-file-path)
(split-string (buffer-string) "\n" t))))
(history (completing-read "[EAF/browser] Search || URL || History: " history-list)) (history (completing-read "[EAF/browser] Search || URL || History: " history-list))
(history-url (when (string-match "[^\s]+$" history) (history-url (when (string-match "[^\s]+$" history)
(match-string 0 history)))) (match-string 0 history))))

Loading…
Cancel
Save