Is there a mime type or deserialiser for JavaScript function like in bookmarklets

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.

2 Likes

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.

  • For example drop it on a single file read only wiki - what can it do?
  • In fact a viewer that exposed the tiddlers within, if it fails it would suggest there is something else in it.
  • You could even ask for a confirmation etc…

The functionality it delivers is substantial.

  • sets of config tiddlers eg preferences.
  • Set of macros and features
  • Drop a set of tiddlers on a wiki and store them for application/reapplication with a click
  • Use for rapid distribution of a package on multiple wikis without leaving the browser.

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;

  • This is helpful to know

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 :wink: and should be hidden from TW police eyes :wink:

1 Like

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.

  • A button in advanced search filter to bookmark or capture the result of the filter would be great.

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.

  • I take from your comments @Mohammad that a bookmarklet tiddler could contain a list of tiddlers or a filter for live bookmarklets, not just as captured, recaptured.
  • Also to allow a click in the wiki on the bookmarklet, to apply the tiddlers without it being placed in the bookmarks, a live bookmarklet will only set the tiddlers to what they currently are.
  • me too, multiple times a day.

All, Thanks for your feedback.

  • It has helped me move forward with a new approach see below
  • Sorry I could not work out how to use it.
  • This is a good idea, I have considered it before, it would be good if there were a tiddlywiki standard supporting this so we could distribute tiddlers with their own view template.
  • I previously developed something similar, called local-viewtemplate local-viewtemplate.json (5.0 KB) where with this in place any tiddler can has its own view Template.

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.

  • We already have a deserialiser and an Import process for JSON files so It makes sense to stick with that.
  • Imported JSON can be both plugins and simple JSON, but arguably any JSON can thus be a Bookmarklet.
  • A few fields and or naming can be used on any JSON file or an accompanying tiddler to support a bookmarklet JSON.
  • Each tiddler within the JSON can have a field that determines how a bookmarklet generator could handle that tiddler. Currently its only 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.

  • In your solution which I like, the tiddler contains the javascript function

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?

1 Like

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

  • Use a filter that obtains the list of payload tiddlers
    • This captures the nominated tiddlers as they are at that moment
  • or have a JSON tiddler or File containing tiddlers.
    • This contains tiddlers and their contents as at the time the JSON was built.
    • The $:/Import tiddler and Plugins are already in this form

You can use such tiddler payload bookmarklets;

  • To change settings
  • Have two opposite setting’s to restore.
  • To install one or more plugins or tiddlers on any wiki you have open in a tab
  • Install test data and reinstall as needed
  • Install tiddlers then edit them and save them back to an updated bookmarklet capturing different configuration states
  • Take a temporary backup of one or more tiddlers and save in a bookmarklet, or JSON for that matter.
  • To restore the history list
  • To apply a read only or read update mode

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 answers I have found is just keep the JSON tiddler standard, of type application/json and using the tiddlers caption or title generate a link that can either be;
    • Clicked in wiki to apply the payload to the current wiki, one or more times.
    • Dragged to the bookmarks to reuse on other wikis as well.
    • The JSON tiddler can also be imported, exported, shared online, dragged and dropped between wikis etc…
  • Since a JSON tiddler may have being generated using a list or filter perhaps a de facto standard is to include the filter if available in the same JSON tiddler in the “filter field”.
    • This would allow a second “bookmarklet” to be generated who’s payload is the tiddlers named in the filter in their current state in the current wiki. This is Mohammad’s approach.
  • I have taken this on board and using the JSON tiddler standard makes it easier to see.

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”.

  • It seems to me such functionality is so useful and empowers sharing we need to find a de facto if not de jure standard bookmarklet tool.
  • We then need only share JSON / Bookmarklet tiddlers and files without needing to install anything.