I personally think distributing executable code as bookmarklets shouldn’t be promoted.
Users can’t read the code and most of the time don’t know what’s going on. In combination with TW I would consider it problematic.
I personally think distributing executable code as bookmarklets shouldn’t be promoted.
Users can’t read the code and most of the time don’t know what’s going on. In combination with TW I would consider it problematic.
I understand your concern however;
With the right view, it should be easy to display what the content is, especially since for tiddlywiki they are typically just a payload of tiddlers.
The functionality it delivers is substantial.
Bookmarklets do not have an official mime type as far as I know. But you can make your own. The spec has the prs
prefix for such occasions.
You could use text/prs.bookmarklet
and make a parser that knows how to render the code maybe even minify it on the fly.
I tried this but a parser only renders the text content and has no access to other fields like title and this I was only able to make a link with the label “Bookmarklet” while the parsed code was in the href
attribute.
I can post this code if that is what you wanted. I suggested a widget because then not only could you parse the bookmarklet text as a link but also have a way to customize the label.
Sounds interesting @sukima, do share if it not too much trouble. I may be able to build more around it.
I think this plugin is what you might want. Least it is my interpretation on the solution:
https://tritarget.org/wikis/bookmarklet-plugin.html
I included the minified version of Uglify.js for the browser adding a exports
and a custom Widget to output an <a>
tag.
Thanks @sukima this is perhaps part of the solution;
Ultimately I want a standardised bookmarklet tiddler standard to support the interchange of solutions.
Are you are already generating the tiddler json which contains the bookmarklet? If so, then what about generating it already in the <a href=...
form? That way you don’t need to depend on any mime type or deserializer in the core. Your json is completely self-contained.
Or if for some reason you are forced or prefer to keep the javascript:(function()...
format you could include your own view template in the payload. That view template could take care of displaying the <a href=...
form when the tiddler body matches javascript:
.
In TW-Script, I use a wikitext macro to create a link contains a bookmarklet of filtered tiddlers and you can simply drag and drop that link to your browser bookmark bar.
I use this to inject a set of my selected plugins (like utility, links-to-tab, commander, shiraz, favorites, …) and few configuration tiddlers when I visit other TW on the web.
This is kind of spying and should be hidden from TW police eyes
Note that you can call the macro with different filter and create several bookmarklets as you like.
This is what we may call a live link, that is it drags the tiddlers in their current state into the bookmarklet, and this is a great way to save the current state. Once a bookmarklet they are frozen in time.
However if we want to capture tiddlers with specific values, not the one they happen to have at that moment, they need to be frozen in time if we want to exchange them for others to use. We may not care what the values are in someone else’s wiki, only that when used, it uses tiddler in the state captured in the “bookmarklet tiddler” I created elsewhere.
The point here is to allow us to share bookmarklets via the exchange of tiddlers.
All, Thanks for your feedback.
A new approach
I have realised that the key to a bookmarklet is a tiddler filter, and those same tiddlers captured in time - which ultimately is just a JSON of tiddlers.
tw.wiki.addTiddlers
but perhaps we could add other functions such as delete tiddler, or other functions?Let me know if you want to collaborate on a solution.
So I suppose I have answered the OT
Is there a mime type or deserialiser for JavaScript function like in bookmarklets?
The answer is yes, application/json
I will keep this thread alive because, I intend to expand the functionality around the application/json type to support bookmarklets.
If you have insight as to how tiddlywiki handles the mime types in general an/or specifically application/json please share.
Please elaborate. That link is a standard TiddlyWiki plugin with examples and uses standard TiddlyWiki widget syntax, How could its use be more approachable?
Overloading an already supported mimetype seems like a recipe for disaster. If need why not use a custom mimetype like text/prs.bookmarklet
and then you can make your own parser for said type.
bookmarklets are encoded javascript sources. They have a javascript:
prefix, The content is one line and URI encoded. Since JavaScript has built-in ASI (Auto Semi-colon Insertion) it is difficult to cleanly convert a JS file into a single line using a simple join algorithm. The better solution is to use a minifier which not only would compact to one line with ASI taken into account but also make the output much smaller as it knows how to intelligently compact identifiers. That adds the advantage that a bookmarklet is less likely to reach any browsers’ URL size limit.
Since converting any JS source via a minifier and then URI encoding it would either need to be done before adding it to a tiddlywiki for use or have it compiled within the tiddlywiki itself. In my plugin example I included uglify.js to minify in the browser so no build tools would be necessary.
The plugin adds a widget that knows how to take a tiddler of type application/javascript
and takes its text
field run it through uglifyjs, encode it with encodeURIComponent, and prefix javascript:
to the string and that is the rendered output.
To make it a clickable link that a user can either right click to add as bookmark or drag drop to the bookmark bar it renders it as an <a href="...">
tag.
Originally I concidered making a parser for a custom MIME type like text/prs.bookmarklet
but that would only provide the compiled textual bookmarklet output and not a interactive link like you would want. Granted through a series of macro calls you could use that to show such a link (see makedatauri
macro) but the usage would be way more complicated. With a widget you add a tiddler of type application.javascript
and where you want a bookmarklet link use <$bookmarklet tiddler="tiddler title"/>
It’s not what I though it would be, but that is OK.
To quote myself,
I would ask you @sukima to work with me to extend the functionality of your bookmarklet widget for it to turn an application/JSON tiddler into a payload bookmarklet, or alternatively accept a filter parameter and do the same as Mohamad’s solution, and keep your existing functionality.
The URI encoding already collapses everything into a single line. Are there still cases where the URI encoding is not enough (other than size limit issues)? If not, then a bookmarklet can be created without using a minifier:
First I created a template tiddler titled “bookmarklet template” like this:
<a href={{{[all[current]get[text]] +[encodeuri[]addprefix[javascript: (() => {]addsuffix[})()]]}}}>{{!!title}}</a>
Then a simple javascript tiddler titled “alert and console log javascript example” with contents like this:
alert("testing")
console.log("testing")
I applied the template to the javascript like this:
{{alert and console log javascript example||bookmarklet template}}
The resulting html looks like this:
<a href="javascript: (() => {alert(%22testing%22)%0Aconsole.log(%22testing%22)})()">alert and console log javascript example</a>
Clicking on that link does display an alert and a console log message. Maybe for more complicated javascript, this approach will fall apart?
That is an interesting solution. One I hadn’t considered. I like it. +1 well done.
I’m truly sorry but I don’t think we are talking the same things. That list you have seems to be discussing a different problem then displaying bookmarklet code a <a>
tags. I’m not saying anything is wrong there I just do not understand much of what you ask in the context of bookmarklets. Perhaps our vocabularies are different?
The key difference between my proposed bookmarklets and those discussed by others here is using bookmarklets to carry a payload of tiddlers. The thing is config, tweaks and appearance are just a start, we can set almost anything using a set of tiddlers containing specific values. See the top post for an example. Click on such a bookmark and the “embedded” tiddlers are installed silently.
There are two key ways to prepare the payload tiddlers
You can use such tiddler payload bookmarklets;
The key issue I have found is it is hard to share and transfer such bookmarklets. I want to develop an approach that we can all share tiddler package bookmarklets with a consistent format and standard.
The remaining challenge, which is why I started this thread, is the best way to update the core, provide a core plugin or share bookmarklet JSON’s with additional tiddlers to support “Bookmarklets”.