<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet href="/assets/atom.xsl" type="text/xsl"?><feed
	xmlns="http://www.w3.org/2005/Atom"
	xmlns:thr="http://purl.org/syndication/thread/1.0"
	xml:lang="en-US"
	><title>Sacha Chua - category - web</title>
	<subtitle>Emacs, sketches, and life</subtitle>
	<link rel="self" type="application/atom+xml" href="https://sachachua.com/blog/category/web/feed/atom/index.xml" />
  <link rel="alternate" type="text/html" href="https://sachachua.com/blog/category/web" />
  <id>https://sachachua.com/blog/category/web/feed/atom/index.xml</id>
  <generator uri="https://11ty.dev">11ty</generator>
	<updated>2023-01-30T16:02:57Z</updated>
<entry>
		<title type="html">Using Spookfox to scroll Firefox up and down from Emacs</title>
		<link rel="alternate" type="text/html" href="https://sachachua.com/blog/2023/01/using-spookfox-to-scroll-firefox-up-and-down-from-emacs/"/>
		<author><name><![CDATA[Sacha Chua]]></name></author>
		<updated>2023-01-30T16:02:57Z</updated>
    <published>2023-01-30T16:02:57Z</published>
    <category term="web" />
<category term="emacs" />
		<id>https://sachachua.com/blog/2023/01/using-spookfox-to-scroll-firefox-up-and-down-from-emacs/</id>
		<content type="html"><![CDATA[<p>
I open lots of pages in the process of making Emacs News. I like to
open the pages in Mozilla Firefox, but I want the keyboard focus to
stay with Emacs so that I can quickly categorize the links. I also
sometimes want to scroll the page up or down. While reading the
<a href="https://bitspook.in/blog/reading-and-not-forgetting/">Reading, and not forgetting</a> post, I came across <a href="https://github.com/bitspook/spookfox">Spookfox</a>, which
bridges Emacs and Firefox using an Firefox add-on and websockets.
After I started spookfox and connected to it by clicking on the
extension in Firefox, I was able to interact with it from Emacs Lisp.
I feel a little nervous about it security-wise, but at least it's only
listening on the local port. There might be another way to do it with
the Marionette support in Firefox, but I haven't looked into it yet.
</p>

<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span class="org-keyword">use-package</span> <span class="org-constant">spookfox</span>
  <span class="org-builtin">:quelpa</span> (spookfox <span class="org-builtin">:fetcher</span> github <span class="org-builtin">:repo</span> <span class="org-string">"bitspook/spookfox"</span>
                    <span class="org-builtin">:files</span> (<span class="org-string">"lisp/*.el"</span> <span class="org-string">"lisp/apps/*.el"</span>))
  <span class="org-builtin">:when</span> my-laptop-p
  <span class="org-builtin">:config</span>
  (<span class="org-keyword">require</span> <span class="org-highlight-quoted-quote">'</span><span class="org-constant">spookfox-tabs</span>)
  (<span class="org-keyword">require</span> <span class="org-highlight-quoted-quote">'</span><span class="org-constant">spookfox-org-tabs</span>)
  (<span class="org-keyword">require</span> <span class="org-highlight-quoted-quote">'</span><span class="org-constant">spookfox-js-injection</span>)
  (add-to-list <span class="org-highlight-quoted-quote">'</span><span class="org-highlight-quoted-symbol">spookfox-enabled-apps</span> <span class="org-highlight-quoted-quote">'</span><span class="org-highlight-quoted-symbol">spookfox-tabs</span>)
  (add-to-list <span class="org-highlight-quoted-quote">'</span><span class="org-highlight-quoted-symbol">spookfox-enabled-apps</span> <span class="org-highlight-quoted-quote">'</span><span class="org-highlight-quoted-symbol">spookfox-org-tabs</span>)
  (add-to-list <span class="org-highlight-quoted-quote">'</span><span class="org-highlight-quoted-symbol">spookfox-enabled-apps</span> <span class="org-highlight-quoted-quote">'</span><span class="org-highlight-quoted-symbol">spookfox-js-injection</span>)
  <span class="org-comment-delimiter">;; </span><span class="org-comment">(spookfox-init) ; don't automatically enable it; run (spookfox-init) to manually enable</span>
  )
</pre>
</div>

<p>
Anyway, this code seems to do the job of scrolling my Firefox window:
</p>

<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span class="org-keyword">defun</span> <span class="org-function-name">my-spookfox-scroll-down</span> ()
  (<span class="org-keyword">interactive</span>)
  (spookfox-eval-js-in-active-tab <span class="org-string">"window.scrollBy(0, document.documentElement.clientHeight);"</span>))

(<span class="org-keyword">defun</span> <span class="org-function-name">my-spookfox-scroll-up</span> ()
  (<span class="org-keyword">interactive</span>)
  (spookfox-eval-js-in-active-tab <span class="org-string">"window.scrollBy(0, -document.documentElement.clientHeight);"</span>))

(global-set-key (kbd <span class="org-string">"C-s-v"</span>) <span class="org-highlight-quoted-quote">'</span><span class="org-highlight-quoted-symbol">my-spookfox-scroll-down</span>)
(global-set-key (kbd <span class="org-string">"C-s-S-v"</span>) <span class="org-highlight-quoted-quote">'</span><span class="org-highlight-quoted-symbol">my-spookfox-scroll-up</span>)
</pre>
</div>

<p>
This code opens a tab without switching keyboard focus away from Emacs:
</p>

<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span class="org-keyword">defun</span> <span class="org-function-name">my-spookfox-background-tab</span> (url <span class="org-type">&amp;rest</span> args)
  <span class="org-doc">"Open URL as a background tab."</span>
  (<span class="org-keyword">if</span> spookfox&#45;&#45;connected-clients
      (spookfox-tabs&#45;&#45;request (cl-first spookfox&#45;&#45;connected-clients) <span class="org-string">"OPEN_TAB"</span> <span class="org-highlight-quoted-quote">`</span>(<span class="org-builtin">:url</span> ,url))
    (browse-url url)))
</pre>
</div>

<p>
My Emacs News code for processing my upvoted Reddit posts can
automatically grab the links from Reddit link posts, but sometimes
people post Reddit text or image posts and then include the link to
the actual project in the post body or a comment instead.
</p>

<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span class="org-keyword">defun</span> <span class="org-function-name">my-spookfox-get-links</span> ()
  (seq-uniq
   (spookfox-eval-js-in-active-tab <span class="org-string">"[...(document.querySelector('[data-testid=post-container]')?.parentElement || document).querySelectorAll('</span><span class="org-string"><span class="org-constant">a</span></span><span class="org-string">')].map(a =&gt; a.href).filter(a =&gt; a &amp;&amp; !a.match(/redd</span><span class="org-string"><span class="org-warning">\</span></span><span class="org-string">.?it/) &amp;&amp; !a.match(window.location.host))"</span> t)))
<span class="org-comment-delimiter">;;</span><span class="org-comment">https://emacs.stackexchange.com/questions/41801/how-to-stop-completing-read-ivy-completing-read-from-sorting</span>
(<span class="org-keyword">defun</span> <span class="org-function-name">my-presorted-completion-table</span> (completions)
  (<span class="org-keyword">lambda</span> (string pred action)
    (<span class="org-keyword">if</span> (eq action <span class="org-highlight-quoted-quote">'</span><span class="org-highlight-quoted-symbol">metadata</span>)
        <span class="org-highlight-quoted-quote">'</span>(metadata
         (cycle-sort-function . identity)
         (display-sort-function . identity))
      (complete-with-action action completions string pred))))

(<span class="org-keyword">defun</span> <span class="org-function-name">my-spookfox-insert-link-from-page</span> ()
  (<span class="org-keyword">interactive</span>)
  (<span class="org-keyword">let*</span> ((links (my-spookfox-get-links))
         (link (completing-read
                <span class="org-string">"Link: "</span>
                (my-presorted-completion-table
                 links))))
    (insert (org-link-make-string link (my-page-title link)))))
</pre>
</div>

<div class="note">This is part of my <a href="https://sachachua.com/dotemacs#spookfox">Emacs configuration.</a></div><p>You can <a href="mailto:sacha@sachachua.com?subject=Comment%20on%20https%3A%2F%2Fsachachua.com%2Fblog%2F2023%2F01%2Fusing-spookfox-to-scroll-firefox-up-and-down-from-emacs%2F&body=Name%20you%20want%20to%20be%20credited%20by%20(if%20any)%3A%20%0AMessage%3A%20%0ACan%20I%20share%20your%20comment%20so%20other%20people%20can%20learn%20from%20it%3F%20Yes%2FNo%0A">e-mail me at sacha@sachachua.com</a>.</p>]]></content>
		</entry>
</feed>