<p dir="auto">It seems that the string from DOM is encoded in UTF-16, then use with node's <code class="notranslate">fs</code> module, it can not handle properly.</p>
<p dir="auto">For example:</p>
<div class="highlight highlight-text-html-basic notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&lt;div id=&quot;demo&quot;&gt;abc&lt;/div&gt;"><pre class="notranslate"><span class="pl-kos">&lt;</span><span class="pl-ent">div</span> <span class="pl-c1">id</span>="<span class="pl-s">demo</span>"<span class="pl-kos">&gt;</span>abc<span class="pl-kos">&lt;/</span><span class="pl-ent">div</span><span class="pl-kos">&gt;</span></pre></div>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="fs.writeFile(path,document.querySelector('#demo').innerText,function(){
    //xxx
})"><pre class="notranslate"><span class="pl-s1">fs</span><span class="pl-kos">.</span><span class="pl-en">writeFile</span><span class="pl-kos">(</span><span class="pl-s1">path</span><span class="pl-kos">,</span><span class="pl-smi">document</span><span class="pl-kos">.</span><span class="pl-en">querySelector</span><span class="pl-kos">(</span><span class="pl-s">'#demo'</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-c1">innerText</span><span class="pl-kos">,</span><span class="pl-k">function</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">{</span>
    <span class="pl-c">//xxx</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span></pre></div>
<p dir="auto">Then the file written by node is not complete string <code class="notranslate">abc</code>. By using the <code class="notranslate">Buffer</code> module, it showed the the <code class="notranslate">innerText</code> content is something like this:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="console.log(new Buffer(document.querySelector('#demo').innerText));
//Buffer {0: 97, 1: 0, 2: 98, 3:0, 3: 99, 4: 0}"><pre class="notranslate"><span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-k">new</span> <span class="pl-v">Buffer</span><span class="pl-kos">(</span><span class="pl-smi">document</span><span class="pl-kos">.</span><span class="pl-en">querySelector</span><span class="pl-kos">(</span><span class="pl-s">'#demo'</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-c1">innerText</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-c">//Buffer {0: 97, 1: 0, 2: 98, 3:0, 3: 99, 4: 0}</span></pre></div>
<p dir="auto">Node-webkit has similar problem. Is this correct or a bug?</p>
<p dir="auto">Workaround: <code class="notranslate">JSON.parse(JSON.stringify(xxx.innerText))</code> or if the string is <strong>changed</strong> by string method like <code class="notranslate">substr</code> etc.</p>
<p dir="auto">Hey.</p>
<p dir="auto">When retrieving a hexadecimal string from the user's clipboard and converting it to a Buffer via <code class="notranslate">new Buffer string, 'hex'</code>, it seems that the second half of the string isn't parsed correctly and defaults to a random byte string.</p>
<div class="highlight highlight-source-coffee notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="document.addEventListener 'paste', (evt) -&gt;
  str1 = evt.clipboardData.getData 'Text'
  str2 = 'F1F2F3F4'
  buf1 = new Buffer str1, 'hex'
  buf2 = new Buffer str2, 'hex'
  str1 === str2 # =&gt; true
  buf1.toString 'hex' # =&gt; 'f1f20000'
  buf2.toString 'hex' # =&gt; 'f1f2f3f4'"><pre class="notranslate"><span class="pl-c1">document</span>.<span class="pl-c1">addEventListener</span> <span class="pl-s"><span class="pl-pds">'</span>paste<span class="pl-pds">'</span></span>, (<span class="pl-smi">evt</span>) <span class="pl-k">-&gt;</span>
  <span class="pl-v">str1</span> <span class="pl-k">=</span> <span class="pl-smi">evt</span>.<span class="pl-smi">clipboardData</span>.<span class="pl-en">getData</span> <span class="pl-s"><span class="pl-pds">'</span>Text<span class="pl-pds">'</span></span>
  <span class="pl-v">str2</span> <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">'</span>F1F2F3F4<span class="pl-pds">'</span></span>
  <span class="pl-v">buf1</span> <span class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">Buffer</span> str1, <span class="pl-s"><span class="pl-pds">'</span>hex<span class="pl-pds">'</span></span>
  <span class="pl-v">buf2</span> <span class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">Buffer</span> str2, <span class="pl-s"><span class="pl-pds">'</span>hex<span class="pl-pds">'</span></span>
  str1 <span class="pl-k">==</span><span class="pl-k">=</span> str2 <span class="pl-c"><span class="pl-c">#</span> =&gt; true</span>
  <span class="pl-smi">buf1</span>.<span class="pl-c1">toString</span> <span class="pl-s"><span class="pl-pds">'</span>hex<span class="pl-pds">'</span></span> <span class="pl-c"><span class="pl-c">#</span> =&gt; 'f1f20000'</span>
  <span class="pl-smi">buf2</span>.<span class="pl-c1">toString</span> <span class="pl-s"><span class="pl-pds">'</span>hex<span class="pl-pds">'</span></span> <span class="pl-c"><span class="pl-c">#</span> =&gt; 'f1f2f3f4'</span></pre></div>