Posts Tagged ‘javascript’

Javascript bookmarklet to delete first publication from CCV web form

Friday, June 17th, 2016

The Canadian Common CV webform failed to upload my publications correctly. But it doesn’t allow batch deletion of items so to delete each one at a time I’d have to click the little trash can and click “Yes” and then the page reloads. To avoid this I wrote a little javascript bookmarklet:

eval($("img[alt^=Delete]")[0].getAttribute('onclick').replace('preventBubbling(event);askDeleteBefore','processSubmit').replace('return false;',''))

Drop-in replacement header for switching from MathJax to KaTeX in markdown

Friday, November 7th, 2014

I wanted to try out switching to KaTeX from MathJax for our libigl tutorial. KaTeX needs a little bit of javascript besides the including the script to know to iterate over all class=math tags and render the latex code.

Also, it seems that markdown translates \\(x+y\\) into <span class=math>\(x+y\)</span> and the \( parenthesis throw off the KaTeX. You get an error like:

ParseError: KaTeX parse error: Expected 'EOF', got '\(' at position 2: \(x+y

So my header to switch from mathjax simply iterates over all math tags on document load using jquery and strips these parenthesis (which I expect on all tags):

<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.1.1/katex.min.css">
<script src="http://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.1.1/katex.min.js"></script>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script>
$(function(){
  $(".math").each(function() {
    var texTxt = $(this).text();
    texTxt =  texTxt.slice(2,-2);
    el = $(this).get(0);
    if(el.tagName == "DIV"){
        addDisp = "\\displaystyle";
    } else {
        addDisp = "";
    }
    try {
        katex.render(addDisp+texTxt, el);
    }
    catch(err) {
        $(this).html("<span class='err'>"+err);
    }
  }); 
}); 
</script>

I didn’t invest much more into bulletproofing this because it seems like KaTeX is still in its infancy in terms of LaTeX support. It doesn’t support unicode characters (e.g. ∑, ∆, π) or common tags like \mathbf or \begin{cases}. Hopefully it will eventually, it is much faster than MathJax for the simple equations.

Expand all comments and replies on Facebook

Friday, October 10th, 2014

A couple times I’ve been frustrated that Facebook announces “So and so replied to a comment on this.” above a post with 100K comments. If I want to see what So and so wrote then I’m stuck clicking “View more comments” and “View more replies” until I can find So and so.

This bookmarklet will expand all replies and comments:

javascript:function find_more_and_click(){function i(e){if(e.length<=0){return}e[0].click();window.setTimeout(function(){i(e.splice(1))},800)}var e=document.getElementsByClassName("UFIPagerLink");var t=[];for(var n=0;n<e.length;n++){if(e[n]&&(e[n].text=="View more replies"||e[n].text=="View more comments")){t.push(e[n])}}var r=t.length;i(t);if(r>0){window.setTimeout(function(){find_more_and_click()},800)}}find_more_and_click()

Here’s the source

function find_more_and_click()
{
  var buttons = document.getElementsByClassName('UFIPagerLink');
  var more = [];

  // Select only the View more ... buttons.
  for (var i = 0; i < buttons.length; i++) {
      if (buttons[i] && (buttons[i].text == 'View more replies' || buttons[i].text == 'View more comments')) {
          more.push(buttons[i]);
      }
  }
  var more_length = more.length;

  function clickFn(more) {
      if (more.length <= 0) {
          return;
      }
      more[0].click();

      // Wait for each Like to be processed before trying the next.
      // Facebook enforces this requirement.
      window.setTimeout(function() {
          clickFn(more.splice(1));
      }, 800);
  }
  clickFn(more);
  if(more_length>0)
  {
    window.setTimeout(function(){find_more_and_click();},800);
  }
}
find_more_and_click();

Source

Open IEEE pdf without banner

Tuesday, February 11th, 2014

The IEEE Xplore library has a very annoying way of serving up PDFs. The page has two frames, one’s a small banner at the top and the bother is the PDF. Both Safari and Chrome keep the banner in focus so when I try to zoom I’m just zooming the banner. Here’s a javascript bookmarklet to open that second frame as a proper page:

javascript:(function(){window.location.href=document.getElementsByTagName("frame")[1].src;})()

Scrape 3D model from Sketchfab

Wednesday, October 16th, 2013

SketchFab has many 3D models, like this bulldog. Open up their webgl viewer and extract the following Javascript variables to recover the model vertices and faces.

// Copy result of this:
view3D._scene.children[2].children[0].children[0].children[0].children[0].children[0].children[0].children[1].attributes.Vertex._elements
// to U


for each primitive p
  // Copy result of this
  view3D._scene.children[2].children[0].children[0].children[0].children[0].children[0].children[0].children[1].primitives[p].indices._elements
  // to P{p+1}.indices, and 
  view3D._scene.children[2].children[0].children[0].children[0].children[0].children[0].children[0].children[1].primitives[p].mode // 5 means TRIANGLE_STRIP, 4 means TRIANGLES
  // to P{p+1}.mode

Then in matlab you can issue:

V = reshape(U,3,[])';
F = [];
for p = 1:numel(P)
  switch P{p}.mode
  case 4
    F = [F;reshape(P{p}.indices+1,3,[])'];
  case 5
    pF = triangles_from_strip(P{p}.indices+1);
    dblA = doublearea(V,pF);
    F = [F;pF(dblA>0,:)];
  end
end

Then you can save it to a obj, off or stl file or just render it in MATLAB:

sketchfab bulldog in matlab

Bookmarklet to toggle between local and live website

Wednesday, August 14th, 2013

I administer our groups website and I often work with a local version of the page. Inevitably I end up toggling between the local and live versions of the page. Here’s a bookmarklet that switches between addresses starting with localigl and igl.ethz.ch. So if I’m viewing the live page at http://igl.ethz.ch/publications it’ll switch to http://localigl/publications:


javascript:(function(){window.location.href=window.location.href.replace(/localigl|igl.ethz.ch/g,function(w){switch(w){case "localigl": return "igl.ethz.ch"; case "igl.ethz.ch": return "localigl";}});})()

Buttons inside forms are default type submit

Thursday, July 18th, 2013

I was frustrated that my undo button was submitting my form. My HTML code looked something like:


<form action="." method="GET">
...
<button onclick="undo();">Undo</button>
<button onclick="redo();">Redo</button>
...
</form>

At first I thought it was a javascript problem and some how my undo() function was submitting the form erroneously. But lo and behold this is the default behavior of a button inside of a form.

So, I needed to change my HTML to:


<form action="." method="GET">
...
<button type=button onclick="undo();">Undo</button>
<button type=button onclick="redo();">Redo</button>
...
</form>

Google search current page bookmarklet

Thursday, May 23rd, 2013

Here’s a javascript bookmarklet for googlin’ the title of the current page your viewing:


javascript:(function(){window.location.href="http://google.com/search?q="+document.title;})()

Drag me onto your bookmark bar.

Then when you’re at a sit that blocks regular visiting but allows entrance through a google search—cough, cough, nytimes.com—you can quickly google the title and click the inevitably top link.

DOM Exception in XMLHttpRequest

Thursday, April 18th, 2013

I was getting this error when trying to set up an XMLHttpRequest using javascript:


INVALID_STATE_ERR: DOM Exception 11: An attempt was made to use an object that is not, or is no longer, usable

The problem was that I was calling req.setRequestHeader(...) before calling req.open(...). Reversing the order fixed the problem.

OBJ reader, mesh viewer in browser with WebGL

Wednesday, April 10th, 2013

mesh in browser

I’m getting around to release source code and demos for my PhD projects and I thought it would be cool to try out writing those demos using WebGL. After toying around (for a few years now), I have a little proof of concept.

This little mesh viewer can load an .obj mesh file (just bare basics of the format) from (1) the remote server, (2) a selected file, or (3) a drag-and-drop file. Just drag an .obj file from your folder onto the viewer! For now there’s not much interface, just a simple zoom mechanism. But I’m going to build on this and keep updated the page.

The source code is heavily borrowed from around the web. See the .js sources.