Some bookmarklets

A custom tea timer for https://smithtea.com/pages/know-your-tea#brewing-instructions:

javascript:(function(){var timestr=window.prompt("Time in sec?","120"); if(timestr===null) /*Cancel*/ return; clock.stop(); clock.reset(); var t = +parseInt(timestr,10) || 0; /*https://stackoverflow.com/a/7540412/2877364*/ if(t<=0) return; clock.setTime(t); clock.start(); })();

A message I have found myself typing frequently on Stack Overflow: ūüôā

javascript:!function(){var t=document.querySelector.bind(document),o=t(".edit-post");if(o){var e=t(".js-add-link.comments-link");if(e){e.click();var a=t('textarea[name="comment"]');a&&(a.value="Welcome to the site! Check out the [tour](https://stackoverflow.com/tour) and the [how-to-ask page](https://stackoverflow.com/help/how-to-ask) for more about asking questions that will attract quality answers. You can [edit your question]("+o.href+") to include more information.",a.scrollTop=a.scrollHeight,a.focus())}}}();

As compressed by https://jscompress.com/; non-minified is:

javascript:(function(){
 var qsel = document.querySelector.bind(document);
 var edit_link = qsel('.edit-post');
 if(!edit_link) return;
 var add_comment = qsel('.js-add-link.comments-link');
 if(!add_comment) return;
 add_comment.click();
 var textbox = qsel('textarea[name="comment"]');
 if(!textbox) return;
 textbox.value =
"Welcome to the site! Check out the [tour](https://stackoverflow.com/tour) "+
"and the [how-to-ask page](https://stackoverflow.com/help/how-to-ask) for "+
"more about asking questions that will attract quality answers. You can "+
"[edit your question](" + edit_link.href + ") to include more information."
 textbox.scrollTop = textbox.scrollHeight;
 textbox.focus();
})();

These are the first two I have written.  I  hope they are useful to you, or serve as helpful examples if you are writing your own bookmarklets!

Advertisements

Vim: joining lines having continuation markers

This handy file-renaming script at Perl Monks has source code with red “+” marks marking wrapped lines. I pasted it into Vim and wanted to wrap those lines back to the way they should be. After a bit of fiddling, I got:

:g/^+/execute "norm 0x" | .-1,.j!

The `g/^+/’ finds the lines beginning with a “+”. Then the `execute “norm 0x”‘ deletes the “+” (“0” moves to beginning of line; “x” deletes), “|” marks the next command, and `.-1,.j!’ joins (“j”) the current line (“.”) with the previous line (“.-1”) without whitespace (“!”) added between the lines.

I leave it to you whether I have too much time on my hands.

When programmers get it right

Technology makes our lives better to the extent we don’t have to think about it. ¬†Robert Pirsig makes this point in¬†Zen and the Art of Motorcycle Maintenance, and it’s still true: technology that draws attention to itself has failed (except to a very hard-core audience of which I am probably part ūüėČ ).

For the last two years, I have been using Google Chrome to view PDFs at work.  Dozens a day, usually with at least five open at a time.  For the last two years, every time I have rotated the view clockwise or counterclockwise, my page has shifted off-screen and I have had to scroll to get back to it.  Has that stopped me from doing my job?  No.  Has it drawn undue attention to the underlying technology?  Yes.  Very yes.

I cannot express my delight at finding,¬†after a recent Chrome update, that I wasn’t the only one with this problem ‚ÄĒ and that it has been fixed! ¬†I rotated my PDF and reached for the scrollbar, but then I noticed the underlying technology in a¬†good way: I was still looking at the same page I had been. ¬†Now each day is a bit smoother, and my job is a bit easier, because the fix has reduced, rather than increased, my mental workload.

My sincere thanks to everyone involved in pdfium bug 116297.  Special thanks to thestig at chromium.org for making the fix!  If you read this, thestig, please know you have succeeded.

Note to programmers: sometimes small fixes¬†are big wins. ¬†It took thestig¬†two lines of code to save me several minutes of wasted effort per work day, every work day, possibly for the rest of my career. ¬†Fix the small things ‚ÄĒ your users will thank you!

Why Assumptions are Important

Every piece of technology is built to work provided its assumptions hold.  Every piece of electronics, for example, assumes it will have power.  No power => no function (as famously noted by The IT Crowd).  By breaking those assumptions, you can do some amazing things:

In this video (~36 min), scanlime finds exactly the right microsecond¬†to drop the power supply — just enough! — to cause the processor built into a graphics tablet to dump its entire memory over a USB connection. ¬†This is not a function the tablet was supposed to provide!

By painstakingly, scientifically defeating the assumption of a stable power supply, the tablet’s firmware control program,¬†intended to be kept within the tablet, becomes¬†available to inspect. ¬†And, as scanlime points out at the end of the video, that control program may well open other doors. ¬†Analyzing¬†the control program may reveal¬†other assumptions the tablet makes — assumptions that can be broken to change¬†the tablet’s function to what she wants it to be.

Every system is only functional, secure, reliable, or any good at all,¬†as long as its assumptions hold. ¬†Whenever you think about the latest gizmo,¬†don’t just ask what it will do for you. ¬†Ask what it won’t do for you when you least expect it — when the gizmo¬†assumes wrong.

C++11 with MinGW

Using standard MinGW (cc 4.9.3), I couldn’t compile C++11 test programs without lots of errors. ¬†The answer: use -std=gnu++11 rather than -std=c++11. ¬†Obvious, right?…

Test program:

#include <memory>
#include <iostream>
using namespace std;
int main()
{
    shared_ptr<int> x(new int(42));
    cout << (*x) << endl;
    return 0;
}

Error messages with “c++11”:

$ g++ --std=c++11 -o test test.cpp 
In file included from c:\mingw\include\wchar.h:208:0, 
  from c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\cwchar:44, 
  from c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\bits\postypes.h:40,
  from c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\iosfwd:40,
  from c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\memory:72, from test.cpp:1: 
c:\mingw\include\sys/stat.h:173:14: error: '_dev_t' does not name a type 
struct _stat __struct_stat_defined( _off_t, time_t ); 
             ^

and many, many more (444 lines total).

By contrast:

$ g++ --std=gnu++11 -o test test.cpp
$ ./test.exe
42
$

Much better!

Exercise for the reader: try TDM-GCC and tell me in the comments whether it works with c++11.

Breaking infinite loops in VBA

I do a lot of Visual Basic for Applications (VBA) development in Microsoft Office. Any program can get stuck in an infinite loop, and when my VBA programs have it has always meant data loss. The Internet to the rescue, as of not quite a month ago!

To break VBA code execution, at least in Office 2013, hit Ctrl+Scroll Lock. ¬†Obvious, right? ūüėČ

Huge thanks to¬†MSDN user povidla, who posted this solution in¬†the MSDN forums. ¬†Povidla, if you’re reading this, please comment so I can thank you directly!

Edit I have found that, in some situations, this doesn’t work unless there is a DoEvents call to catch the Ctrl+ScrLk. ¬†If you have a loop that might be infinite, sticking a DoEvents in it is probably a good practice.

Greetings to Stack Overflow users!  I keep hoping that Povidla will find this page.  If you happen to know who Povidla is, please pass along a thank you from me.  Regardless, thanks for reading!