<?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 - tag - skewer</title>
	<subtitle>Emacs, sketches, and life</subtitle>
	<link rel="self" type="application/atom+xml" href="https://sachachua.com/blog/tag/skewer/feed/atom/index.xml" />
  <link rel="alternate" type="text/html" href="https://sachachua.com/blog/tag/skewer" />
  <id>https://sachachua.com/blog/tag/skewer/feed/atom/index.xml</id>
  <generator uri="https://11ty.dev">11ty</generator>
	<updated>2014-11-11T13:00:00Z</updated>
<entry>
		<title type="html">Emacs: Evaluating Javascript and CSS in Chrome using Skewer Mode</title>
		<link rel="alternate" type="text/html" href="https://sachachua.com/blog/2014/11/emacs-evaluating-javascript-css-chrome-using-skewer-mode/"/>
		<author><name><![CDATA[Sacha Chua]]></name></author>
		<updated>2014-11-03T04:41:03Z</updated>
    <published>2014-11-11T13:00:00Z</published>
    <category term="emacs" />
<category term="geek" />
		<id>https://sachachua.com/blog/?p=27577</id>
		<content type="html"><![CDATA[<p>I build a lot of quick prototypes, using Javascript and CSS to build little tools on top of the Jive social business platform. Since Javascript syntax errors could prevent the proper loading of the overview page customization screen and require me to reset the overview page through the admin console, my previous Javascript workflow involved copying and pasting code into Google Chrome's developer console. Most of the time, I used <code>narrow-to-region</code> to focus on just the specific script block or set of functions I was working on, and I used <code>js2-mode</code> to handle syntax highlighting and indentation. Once the Javascript was sorted out, I'd <code>widen</code> to get back to the full HTML, JS, and CSS file, using <code>web-mode</code> for indentation.</p>
<p>Copying code between Javascript buffers and the developer console was a bit of a hassle. I'd use <code>C-x h</code> (<code>mark-whole-buffer</code>) to select the buffer, then <code>C-w</code> to copy it, change over to the Chrome window (possibly wading through a number of tabs and windows to find the right one), find the developer console, click in it, paste the code, and run it. My first step was to define a custom function that copied the whole buffer:</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span class="org-keyword">defun</span> <span class="org-function-name">sacha/copy-buffer</span> ()
  <span class="org-doc">"Copy buffer contents to kill ring."</span>
  (interactive)
  (kill-new (buffer-substring-no-properties (point-min) (point-max))))
(global-set-key (kbd <span class="org-string">"C-c w"</span>) 'sacha/copy-buffer)
</pre>
</div>
<p>I still had to find the Chrome window and paste the code in, though.</p>
<p>My CSS workflow had its own challenges. I used Inspect Elements to look at CSS properties, and then I modified them on the fly. When I was happy with the rules I added or changed, I wrote the corresponding CSS code in my local file. Because I often ended up modifying several elements, it was hard to remember all the changes I needed to make, apply different sets of changes, or apply the changes after reloading the page. I used Stylish to make some of my changes persistent, but that still involved going back and forth between screens.</p>
<p>Since I'll continue to work on web development over the next year (at least!), I thought I'd invest some time into improving my workflow. I'd seen several demos of <a href="https://github.com/skeeto/skewer-mode">Skewer Mode for Emacs</a>, and I'd even given it a try a few times before. I hadn't integrated it into my workflow yet, but it looked like it was definitely worth a try. Skewer allows you to interact with Google Chrome from Emacs. You can send HTML, CSS, and Javascript fragments to your browser.</p>
<p>If you use the included Greasemonkey-compatible script, you can even use this interactive capability on any website. I used the Tampermonkey extension for Google Chrome to run the script. When I tried it on the site I was working on, though, the https/http mismatch resulted in a content security error. It turns out that you need to run <code>chrome &#45;&#45;allow-running-insecure-content</code> in order to let Chrome inject HTTPS sites with the scripts from the local HTTP server that Skewer Mode runs inside Emacs. If you had other Chrome sessions open, you'll want to close them before starting up Chrome with that option. Once I sorted that out, it was easy to run <code>skewer-setup</code>, open a JS file, and start sending code to my browser.</p>
<p>I quickly became a fan of how <code>C-c C-k</code> (<code>skewer-load-buffer</code> in JS, <code>skewer-css-eval-buffer</code> in CSS) let me send my buffer to my browser. I narrowed my buffer to the parts I was working on, wrote some tests using <code>console.assert(...)</code>, and kept the console visible as I coded. I periodically loaded the buffer to check whether my tests passed. I also liked having a proper file identifier and correct line numbers for errors. (It's amazing how small things matter!)</p>
<p>Then to top it all off, I wanted a function that would prepare the source code for easy pasting into an HTML widget:</p>
<pre class="example">&lt;script type="text/javascript"&gt;
// filename
&lt;/script&gt;
</pre>
<p>Since Emacs is Emacs, you can do that. =)</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span class="org-keyword">defvar</span> <span class="org-variable-name">sacha/javascript-test-regexp</span> (concat (regexp-quote <span class="org-string">"/** Testing **/"</span>) <span class="org-string">"</span><span class="org-string"><span class="org-regexp-grouping-backslash">\\</span></span><span class="org-string"><span class="org-regexp-grouping-construct">(</span></span><span class="org-string">.*\n</span><span class="org-string"><span class="org-regexp-grouping-backslash">\\</span></span><span class="org-string"><span class="org-regexp-grouping-construct">)</span></span><span class="org-string">*"</span>)
  <span class="org-doc">"Regular expression matching testing-related code to remove.</span>
<span class="org-doc">See `</span><span class="org-doc"><span class="org-constant">sacha/copy-javascript-region-or-buffer</span></span><span class="org-doc">'."</span>)

(<span class="org-keyword">defun</span> <span class="org-function-name">sacha/copy-javascript-region-or-buffer</span> (beg end)
  <span class="org-doc">"Copy the active region or the buffer, wrapping it in script tags.</span>
<span class="org-doc">Add a comment with the current filename and skip test-related</span>
<span class="org-doc">code. See `</span><span class="org-doc"><span class="org-constant">sacha/javascript-test-regexp</span></span><span class="org-doc">' to change the way</span>
<span class="org-doc">test-related code is detected."</span>
  (interactive <span class="org-string">"r"</span>)
  (<span class="org-keyword">unless</span> (region-active-p)
    (setq beg (point-min) end (point-max)))
  (kill-new
   (concat
    <span class="org-string">"&lt;script type=\"text/javascript\"&gt;\n"</span>
    (<span class="org-keyword">if</span> (buffer-file-name) (concat <span class="org-string">"// "</span> (file-name-nondirectory (buffer-file-name)) <span class="org-string">"\n"</span>) <span class="org-string">""</span>)
    (replace-regexp-in-string
     sacha/javascript-test-regexp
     <span class="org-string">""</span>
     (buffer-substring (point-min) (point-max))
     nil)
    <span class="org-string">"\n&lt;/script&gt;"</span>)))

(define-key js2-mode-map (kbd <span class="org-string">"C-c w"</span>) 'sacha/copy-javascript-region-or-buffer)
</pre>
</div>
<p>So now I can fiddle around with Javascript and CSS, send it to my browser with <code>C-c C-k</code>, and then use <code>C-c w</code> to wrap the Javascript in <code>script</code> tags and prepare it for copying.</p>
<p>Emacs!</p>
<p>You can <a href="https://sachachua.com/blog/2014/11/emacs-evaluating-javascript-css-chrome-using-skewer-mode/#comment">view 3 comments</a> or <a href="mailto:sacha@sachachua.com?subject=Comment%20on%20https%3A%2F%2Fsachachua.com%2Fblog%2F2014%2F11%2Femacs-evaluating-javascript-css-chrome-using-skewer-mode%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>