John Gruber published a helpful JavaScript Bookmarklet Builder which I used to generate my IPA TTS bookmarklet. A few users reported that Chrome choked on the bookmarklet. I found that the bookmarklet builder does not produce the output that Chrome expects. I was using Chrome 15.0.874.54 beta. For example, take this simple script. <code>(function () [...]
John Gruber published a helpful JavaScript Bookmarklet Builder which I used to generate my IPA TTS bookmarklet.
A few users reported that Chrome choked on the bookmarklet. I found that the bookmarklet builder does not produce the output that Chrome expects. I was using Chrome 15.0.874.54 beta.
For example, take this simple script.
<code>(function () {
// what is 1+1?
alert(["1+1=",(1+1)].join(''));
})()
</code>
Gruber’s script produces this bookmarklet: Example 1
javascript:(function%20()%20{alert([%221+1=%22,(1+1)].join(%27%27));})()
If you drag this example to your bookmark bar in Chrome, it will produce this error when invoked.
Uncaught SyntaxError: Unexpected token %
What happened? If you edit the bookmark, you will see that Chrome double-escaped the string,
javascript:(function%2520()%2520%7Balert(%5B%25221+1=%2522,(1+1)%5D.join(%2527%2527));%7D)()
I believe this happened because the string contained some non-escaped characters such as parentheses.
Gruber points out in a footnote that his script intentionally leaves some characters unescaped for the purpose of readability.
It’s unclear to me what characters must be escaped in a bookmarklet URL. Some sources suggest that other punctuation characters, such as brackets and semicolons, ought to be escaped, too, but I can see no practical reason to do so. If you want to be really conservative and escape just about everything, change this line:
uri_escape_utf8($bookmarklet, qq('" \x00-\x1f\x7f-\xff));to:
uri_escape_utf8($bookmarklet);Personally, I prefer to keep the bookmarklet URL itself as readable as possible.
With this change, we get this bookmarklet: Example 2
javascript:%28function%20%28%29%20%7Balert%28%5B%221%2B1%3D%22%2C%281%2B1%29%5D.join%28%27%27%29%29%3B%7D%29%28%29
It may not be as readable as “Example 1″, but it works.
Find my modifications to Gruber’s JavaScript Bookmarklet Builder on GitHub
Let’s say we have a Google App Engine model with two fields, “x” and “y”, and all objects are distinct. We’d like to paginate with the sort order criteria “x ascending, y ascending”. For example, that index might look like this. While paginating, we need to know whether there is a “next page”, because we [...]
Let’s say we have a Google App Engine model with two fields, “x” and “y”, and all objects are distinct.
We’d like to paginate with the sort order criteria “x ascending, y ascending”. For example, that index might look like this.
While paginating, we need to know whether there is a “next page”, because we don’t want to return an empty page unless we have no objects.
To do this, we’ll look-ahead for the next item in our search. So, if the page size is 2, we’ll just fetch 3 items. That extra object will give us the necessary (x, y) coordinates we need to resume the search, that is, to fetch the next page. This object is called the “bookmark”.
Given the (x, y) bookmark, how do we fetch the next page? In index terms, we might construct a prefix string “/$x/$y/”, and return the next rows after that in our index. Unfortunately App Engine doesn’t give us a low-level interface like that, so we have to use filters.
What kind of filters? We want all objects greater than or equal to our given “x”. But, for objects with the same value as “x”, we have to skip those less than our given “y”, because we have already seen them. Clearly we will need filters on both “x” and “y”, but woe, App Engine insists that no more than one distinct field can have a filter expression per query.[1]
Ryan Barrett observed that for a search with n sorted fields, you will need n queries to resume the search from a bookmark. In other words, you need one query for each field.
What does this look like? In our example, we have two sorted fields. To resume the search, the first query will fix “x” and filter on “y”, with “y ascending”. The second query will filter on “x” with “x ascending, y ascending”.
What would that look like on our example index?
Each shaded region is one query, that is, one trip to the datastore. The green objects are the current page. The orange object is the next bookmark. The blue objects are the old pages.
The first query is on a fixed “x” and filters “y”. The second query resumes where the first left off, and filters on “x”. Since the sort orders are consistent with the filters, we obtain a consistent view of the index.
On the fourth search, we happen to get lucky and find our bookmark on the first query, so we can skip the second query.
In general
If you’re sorting on n fields {f0, f2, … fn-1} you will need n queries {q0, q1, … qn-1}.
Query qi will filter fn-i and fix fields {f0, … fn-i-1}.
For n=2, query q0 filters f1 and fixes f0. Query q1 filters f0.
Why don’t you use cursors you slob?
Cursors are great! Except, they don’t immediately answer the question, “Do we have a next page?” If you don’t care then by all means use cursors, they’re much easier to use than bookmarks.
Cursors in App Engine can’t look-ahead. Instead, you have to fetch one object with the query’s next cursor, rather than fetching an extra object in the original query.
On the other hand, cursors are allowed to perform queries which users cannot. Cursors can perform the “prefix string” trick described above. So, a cursor can do in one query what takes us two or more.
So which should you use? That probably depends on how many fields you are sorting on. For a single field, I prefer the look-ahead technique, because it only requires one trip to the datastore. For two fields, either technique requires (at most) two trips, although cursors are usually simpler. For more than two fields, cursors easily win.
[1] This requirement exists to prevent users from requesting filters which the datastore cannot satisfy by scanning consecutive rows in an index. For example, the filters “x > 2 and y > 3″ can specify non-consecutive rows of an index that sorts “x” and “y” ascendingly.
Ringo, Narwhal, Node, and JavaScriptCore support executing scripts with a shebang. Here are some quick examples to get you started. Ringo #!/usr/bin/env ringo print("ringo"); Narwhal #!/usr/bin/env narwhal print("narwhal"); Node #!/usr/bin/env node require("sys").print("node\n"); JavaScriptCore #!/usr/bin/env jsc print("jsc"); JavaScriptCore nightly added support for shebangs on 6 December 2010. Since Node and JavaScriptCore are binaries, you can use [...]
Ringo, Narwhal, Node, and JavaScriptCore support executing scripts with a shebang.
Here are some quick examples to get you started.
Ringo
#!/usr/bin/env ringo
print("ringo");
Narwhal
#!/usr/bin/env narwhal
print("narwhal");
Node
#!/usr/bin/env node
require("sys").print("node\n");
JavaScriptCore
#!/usr/bin/env jsc
print("jsc");
JavaScriptCore nightly added support for shebangs on 6 December 2010.
Since Node and JavaScriptCore are binaries, you can use the full path instead of env.

