Skip to content

Commit

Permalink
Implement setting a cookie file to persist cookies across sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
akirakyle committed Nov 30, 2020
1 parent 117f111 commit f78396b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 25 deletions.
12 changes: 8 additions & 4 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,11 @@ the handling of focus can be pretty wonky.

;; If you want history saved in a different place or
;; Set to `nil' to if you don't want history saved to file (will stay in memory)
(setq webkit-history-filename "~/path/to/webkit-history")
(setq webkit-history-file "~/path/to/webkit-history")

;; If you want cookies saved in a different place or
;; Set to `nil' to if you don't want cookies saved
(setq webkit-cookie-file "~/path/to/cookies")

;; See the above explination in the Background section
;; This must be set before webkit.el is loaded so certain hooks aren't installed
Expand Down Expand Up @@ -220,14 +224,14 @@ to upstream at some point when things stabilize.
#+end_src

* TODO Roadmap (roughly in order of my priorities)
- Browsing sessions/data and cookie management
- Proxy settings
- Enable webkitgtk process sandboxing
- Make search incremental
- Improve insert/focus handling
- Make search incremental
- Enable webkitgtk process sandboxing
- bookmarks.el
- Dark mode
- ~completing-read~ link completion/heading jumping
- Web extensions + Ad block
- Browsing sessions/data and cookie management
- History ~display-table~ mode
- Load progress and favicon on mode line
17 changes: 10 additions & 7 deletions webkit-history.el
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@

(defvar webkit--id)

(defcustom webkit-history-filename (locate-user-emacs-file "webkit-history")
"File to store history of `webkit' sessions."
(defcustom webkit-history-file
(expand-file-name "history" (locate-user-emacs-file "webkit/"))
"File to store history of `webkit' sessions.
Set to `nil' to disable saving history to a file (history will
still be kept in memory)."
:type 'file
:group 'webkit)

Expand Down Expand Up @@ -88,12 +91,12 @@
:last-time (time-convert (current-time) 'integer))))
(unless (string= (webkit-history-item-uri new-item) "about:blank")
(webkit-history-add-item new-item)
(when webkit-history-filename
(when webkit-history-file
(append-to-file (format "%S\n" (webkit-history-item-serialize new-item))
nil webkit-history-filename)))))
nil webkit-history-file)))))

(defun webkit-history-load ()
(with-current-buffer (find-file-noselect webkit-history-filename)
(with-current-buffer (find-file-noselect webkit-history-file)
(goto-char (point-min))
(condition-case nil
(while t
Expand All @@ -103,10 +106,10 @@
(kill-buffer)))

(defun webkit-history-initialize ()
"Setup required data structure and load history from WEBKIT-HISTORY-FILENAME."
"Setup required data structure and load history from WEBKIT-HISTORY-FILE."
(add-hook 'webkit-load-finished-hook #'webkit-history-add)
(setq webkit-history-table (make-hash-table :test 'equal))
(when webkit-history-filename
(when webkit-history-file
(webkit-history-load))
nil)

Expand Down
20 changes: 20 additions & 0 deletions webkit-module.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,25 @@ webkit_enable_javascript (emacs_env *env, ptrdiff_t n,
return Qnil;
}

static emacs_value
webkit_cookie_set_persistent_storage (emacs_env *env, ptrdiff_t n,
emacs_value *args, void *ptr)
{
Client *c = get_client (env, args[0]);
size_t size;
char *file = NULL;
if ((c != NULL) && copy_string_contents (env, args[1], &file, &size))
{
WebKitWebContext *context = webkit_web_view_get_context (c->view);
WebKitCookieManager *cm = webkit_web_context_get_cookie_manager (context);
webkit_cookie_manager_set_persistent_storage
(cm, file, WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
debug_print ("c %p webkit_cookie_set_persistent_storage %s\n", c, file);
}
free (file);
return Qnil;
}

static emacs_value
webkit_resize (emacs_env *env, ptrdiff_t n, emacs_value *args, void *ptr)
{
Expand Down Expand Up @@ -1106,6 +1125,7 @@ emacs_module_init (struct emacs_runtime *ert)
mkfn (env, 1, 1, webkit_search_previous, "webkit--search-previous", "", NULL);
mkfn (env, 1, 1, webkit_start_web_inspector, "webkit--start-web-inspector", "", NULL);
mkfn (env, 2, 2, webkit_enable_javascript, "webkit--enable-javascript", "", NULL);
mkfn (env, 2, 2, webkit_cookie_set_persistent_storage, "webkit--cookie-set-storage", "", NULL);
mkfn (env, 2, 3, webkit_execute_js, "webkit--execute-js", "", NULL);
mkfn (env, 2, 4, webkit_add_user_style, "webkit--add-user-style", "", NULL);
mkfn (env, 1, 1, webkit_remove_all_user_styles, "webkit--remove-all-user-styles", "", NULL);
Expand Down
43 changes: 29 additions & 14 deletions webkit.el
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
(declare-function webkit--search-previous "webkit-module")
(declare-function webkit--start-web-inspector "webkit-module")
(declare-function webkit--enable-javascript "webkit-module")
(declare-function webkit--cookie-set-storage "webkit-module")
(declare-function webkit--execute-js "webkit-module")
(declare-function webkit--add-user-style "webkit-module")
(declare-function webkit--remove-all-user-styles "webkit-module")
Expand All @@ -62,6 +63,8 @@
(declare-function webkit-history-completing-read prompt "webkit-history")

(defconst webkit--base (file-name-directory load-file-name))
(defconst webkit--user-dir (locate-user-emacs-file "webkit/"))
(make-directory webkit--user-dir t)

(defun webkit--file-to-string (filename)
(with-temp-buffer
Expand All @@ -82,30 +85,42 @@

(defcustom webkit-search-prefix "https://duckduckgo.com/html/?q="
"Prefix URL to search engine."
:group 'webkit
:type 'string)
:type 'string
:group 'webkit)

(defcustom webkit-browse-url-force-new nil
"Whether webkit should use always open a new session instead of
reusing a current one."
:group 'webkit
:type 'boolean)
:type 'boolean
:group 'webkit)

(defcustom webkit-own-window nil
"Whether webkit should use its own window instead of
attemptting to embed itself in its buffer. The curretly focused
frame must be display-graphic-p and either x or pgtk when
webkit-new is run in order for embedding to work."
:group 'webkit
:type 'boolean)
:type 'boolean
:group 'webkit)

(defcustom webkit-download-action-alist '((".*" . webkit-download-default))
"Alist similar to `auto-mode-alist' that maps filename patterns
to functions that will handle their download. Elements have the
form of (REGEXP . FUNCTION) where function takes two arguments:
the URL of the download and the corresponding NAME of the file."
:group 'webkit
:type 'alist)
:type 'alist
:group 'webkit)

(defcustom webkit-configuration-directory
(locate-user-emacs-file "url/" ".url/")
"Directory used by the URL package for cookies, history, etc."
:type 'directory
:group 'url)

(defcustom webkit-cookie-file (expand-file-name "cookies" webkit--user-dir)
"File to store cookies of `webkit' sessions.
Set to `nil' to disable saving cookies to a file."
:type 'file
:group 'webkit)

(defvar webkit-mode-map
(let ((map (make-sparse-keymap)))
Expand Down Expand Up @@ -370,7 +385,6 @@ modeline as a part of `mode-name'"
(expand-file-name file directory)))

(defun webkit-download-save-buffer-safe (dir name)
(message "name %S dir %S" name dir)
(let ((file (webkit--make-unique-file-name name dir)))
(goto-char (point-min))
(re-search-forward "\r?\n\r?\n")
Expand All @@ -390,7 +404,6 @@ modeline as a part of `mode-name'"
(switch-to-buffer (current-buffer)))

(defun webkit-download-default-callback (status url name)
(message "url %S name %S" url name)
(if (plist-get status :error)
(error "Unable to download %S" url)
(if (y-or-n-p "Save to disk? Otherwise download will open in temp buffer")
Expand Down Expand Up @@ -435,7 +448,6 @@ Saves download in user's Downloads directory with filename NAME."
(let* ((obj (url-generic-parse-url url))
(path (directory-file-name (car (url-path-and-query obj))))
(name (eww-decode-url-file-name (file-name-nondirectory path))))
(message "name %S" name)
(funcall (assoc-default name webkit-download-action-alist 'string-match)
url name)))

Expand Down Expand Up @@ -533,6 +545,9 @@ Returns the newly created webkit buffer"
webkit--id "webkit--callback-unfocus")
(webkit--add-user-script webkit--id webkit--script)
(webkit--add-user-style webkit--id webkit--style)
(when webkit-cookie-filename
(webkit--cookie-set-storage
webkit--id (expand-file-name webkit-cookie-filename)))
(run-hooks 'webkit-new-hook)
(when url (webkit--load-uri webkit--id url))
(switch-to-buffer buffer))))
Expand Down Expand Up @@ -582,6 +597,9 @@ the default webkit buffer."
(unless (require 'webkit-module nil t)
(error "webkit needs `webkit-module' to be compiled!"))

(when (version< emacs-version "28.0")
(error "webkit requires an Emacs version > 28"))

;;(defun webkit-setup ()
;; "Setup various hooks necessary for webkit to work.
;;`webkit-own-window' must be set to desired value before this is called."
Expand All @@ -601,8 +619,5 @@ the default webkit buffer."
(add-hook 'webkit-progress-changed-functions 'webkit--display-progress)
(add-hook 'kill-buffer-hook #'webkit--kill-buffer)

(when (version< emacs-version "28.0")
(error "webkit requires an Emacs version > 28"))

(provide 'webkit)
;;; webkit.el ends here

0 comments on commit f78396b

Please sign in to comment.