Rewrite browser history, dynamic sorting by popularity

master
Mingde (Matthew) Zeng 6 years ago
parent ae284a4287
commit 40c3a25823
  1. 75
      core/browser.py
  2. 8
      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_list = []
if self.emacs_var_dict["eaf-browser-remember-history"] == "true":
self.history_log_file_path = os.path.join(self.config_dir, "browser", "history", "log.txt") 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.short_url_pattern = re.compile("^(https?|file)://(.+)") 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") 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,34 +531,35 @@ 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) != "":
touch(self.history_log_file_path)
with open(self.history_log_file_path, "r") as f:
lines = f.readlines()
# Throw traceback info if algorithm has bug and protection of historical record is not erased. # Throw traceback info if algorithm has bug and protection of historical record is not erased.
try: try:
new_lines = [] noprefix_new_url_match = re.match(self.noprefix_url_pattern, new_url)
for line in lines: if noprefix_new_url_match is not None:
line_match = re.match(self.history_url_pattern, line) found = False
if line_match != None: for history in self.history_list:
title = line_match.group(1) noprefix_url_match = re.match(self.noprefix_url_pattern, history.url)
url = line_match.group(2) if (noprefix_url_match is not None):
else: noprefix_url = noprefix_url_match.group(2)
title = "" noprefix_new_url = noprefix_new_url_match.group(2)
url = line nopostfix_new_url_match = re.match(self.nopostfix_url_pattern, noprefix_new_url)
if(noprefix_url == noprefix_new_url): # found unique url
short_new_url = re.match(self.short_url_pattern, new_url) history.title = new_title
short_url = re.match(self.short_url_pattern, url) history.url = new_url
if (short_new_url != None and short_url != None and short_url.group(2) != short_new_url.group(2)): history.hit += 0.5
new_lines.append(line) found = True
elif (nopostfix_new_url_match is not None and noprefix_url == nopostfix_new_url_match.group()):
new_lines.append("{0} {1}\n".format(new_title, new_url)) # also increment parent
history.hit += 0.25
if not found:
self.history_list.append(HistoryPage(new_title, new_url, 1))
self.history_list.sort(key = lambda x: x.hit, reverse = True)
with open(self.history_log_file_path, "w") as f: with open(self.history_log_file_path, "w") as f:
f.writelines(new_lines) f.writelines(map(lambda history: history.title + "" + history.url + "" + str(history.hit) + "\n", self.history_list))
except Exception: except Exception:
import traceback import traceback
self.message_to_emacs.emit("Error in record_history: " + str(traceback.print_exc())) self.message_to_emacs.emit("Error in record_history: " + str(traceback.print_exc()))

@ -1264,9 +1264,13 @@ 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
(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)))) (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)

Loading…
Cancel
Save