Jump to content
vitor

AlfredBookmarklet — Use a browser bookmarklet via Alfred

Recommended Posts

Run browser bookmarklets from Alfred, without needing to having them installed in the browser itself.

 

Note that you may need to Allow JavaScript from Apple Events in your Browser.

 

1


For the first step, get this template workflow itself. Though this is not strictly necessary (you can build it yourself) it serves as a starting point with most of the work already done. Proceed to open the workflow in Alfred.


Next, get a browser bookmarklet to convert by copying its link address.


dJ5XgkB.gif

 

2


Run :cleanbookmarkletcode to clean the code in your clipboard. It performs substitutions necessary to avoid problems when pasting the code in the next step.


oIDEVcd.png

 

3

 

Open the Arg and Vars node and paste the code.


9SPYkmM.gif


Extra


If you’re not new to Alfred, you likely won’t need these steps as you’ll know what to do.

 

4


The template includes both a Keyword and a Hotkey nodes to run the code. You can delete either one by clicking on it and pressing ⌫.


PRpCHu3.gif

 

5


If you choose to use the workflow via Keyword, do not forget to set it up.


ENFYAAe.gif


6


Lastly, edit the workflow’s details and its icon. For completeness it’s pre-filled with my details. Feel free to edit them.


KxIFI2A.png

 

Download | Source

Edited by vitor

Share this post


Link to post

This should work with Safari if allow javascript from smart search field is checked (in the develop menu - last listed)... correct? I never have much luck with bookmarklets but this would be a lifesaver if I can get it going. I will tinker if you say I 'might' have success... Cheers and thanks as always Vitor.

Share this post


Link to post

Update.

Added support for Safari.

twinpeaks — apparently, by doing it via applescript we can get Safari to execute the javascript even without setting that option. The workflow now supports it, so if you download it now it will allow you to do that.

Share this post


Link to post

It should be possible yes, although there are some implementation considerations (like it possibly making this slower, since it’d have to search for the bookmarklets somewhere with each run). The goal with this, however, is not to be an additional way to run bookmarklets, but a replacement. It was done with the specific intent of catering to users who do not have (and even avoid) them, but would occasionally need/like to run a particular one, for which there is no alternative (such as a browser extension).

In addition, it currently requires only two steps in the case of a keyword (type a command, run it), and one in the case of a hotkey, while that solution would require an extra step in the case of keywords, and two in the case of hotkeys (type command / press hotkey, pick a bookmarklet from a list, run it). As it is, it involves a bit of extra initial manual work, but it is more rapidly accessible later. As stated, the target user for this should not have a lot of bookmarklets, so I believe this to work better with that in mind.

Share this post


Link to post

The only bookmarklets I have setup with this system are squirt (the example in the post), and a custom one I’ve made for the very specific purpose of helping with merging pull requests more efficiently in homebrew-cask. AlfredBookmarklet is a refinement on those rough experiments, but it should be able to run any bookmarklet you throw at it, as long as the “no double quotes” condition is met.

If you find one that does’t work, I’d like to know so I can work on improving this.

Share this post


Link to post

Can you post your list of bookmarklets, so that it will give idea of which are good and which ones I can use it.

There's a list here.

Webapps like Instapaper/Pocket, Pinboard, RSS readers and most others that use URLs provide bookmarklets for you to save/add pages to their services.

I use them a lot on my iPhone, too, to send URLs to various apps as a way to get around iOS's lack of Android's refined sharing model.

Edited by deanishe

Share this post


Link to post

I just want to point out that I had to encode bookmarklets (replace %20 witch space etc.) to get this work in Safari. I have used this to do it automatically.

 

Then it worked perfectly.

 

Thanks.

Share this post


Link to post
staudi could you please give an example of such a bookmarklet, so I can run some tests for decoding, to include it directly in the workflow?

Share this post


Link to post

Thank you for the workflow! I have been trying to get this to work with two bookmarklets that open the current Safari page in an external application, though I have been unsuccessful. However, I have no background in JavaScript and little knowledge of AppleScript. 

 

Is it possible to have this workflow trigger a bookmarklet of this type? If so, any assistance in making this happen would be much appreciated.

Share this post


Link to post

Any bookmarklet you can use in these browsers should work with the workflow. Could you post a link to them, so I can inspect them?

Share this post


Link to post

Try this for Papers

if(navigator.userAgent.indexOf('Safari') >= 0){s=getSelection();}else{s=document.selection?document.selection.createRange().text:document.getSelection();}document.location.href='papers2://url/'+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title)+'&selectedText='+encodeURIComponent(s);

And this for ReadCube

tell application "Safari" to tell front document to do JavaScript "javascript:var readcube_bookmarklet_root_url='http://content.readcube.com/'; (function(){var _rcscript=document.createElement('SCRIPT');_rcscript.type='text/javascript';_rcscript.src='http://content.readcube.com/assets/bookmarklet_downloader.js';document.getElementsByTagName('head')[0].appendChild(_rcscript);})();"

 
Both were tested and are working. The first one was related to staudi’s mentioned issue, so now I have a bookmark to test on, and should release a fix soon. The second one was left pretty much unchanged, so I’m not sure what the issue you were having is (tried it on this nature.com article).

Share this post


Link to post

Updated the workflow to be able to clean bookmarklet code that is url encoded (substituted _convertquotes with _cleanbookmarklet).

Share this post


Link to post

Hey Vitor-

 

Use this workflow daily. Thanks.

 

I've run into an error with one bookmarklet that I use to post to Wordpress. The Linkmarklet Wordpress plugin generates a bookmarklet depending on the options selected in the plugin. The javascript generated looks like this:

javascript:var d%3Ddocument,w%3Dwindow,e%3Dw.getSelection,k%3Dd.getSelection,x%3Dd.selection,s%3D(e%3Fe():(k)%3Fk():(x%3Fx.createRange().text:0)),f%3D%27http://prodougtivity.com/wp-content/plugins/linkmarklet%27,l%3Dd.location,e%3DencodeURIComponent,u%3Df%2B%27%3Fu%3D%27%2Be(l.href.replace(new RegExp(%27(https%3F:%5C/%5C/)%27,%27gm%27),%27%27))%2B%27%26t%3D%27%2Be(d.title)%2B%27%26s%3D%27%2Be(s)%2B%27%26v%3D4%26m%3D%27%2B(((l.href).indexOf(%27https://%27,0)%3D%3D%3D0)%3F1:0)%3Ba%3Dfunction()%7Bif(!w.open(u,%27t%27,%27toolbar%3D0,resizable%3D1,scrollbars%3D1,status%3D1,width%3D720,height%3D570%27))l.href%3Du%3B%7D%3Bif (/Firefox/.test(navigator.userAgent)) setTimeout(a, 0)%3B else a()%3Bvoid(0)

And when run through your _cleanbookmarklet script:

javascript:var d=document,w=window,e=w.getSelection,k=d.getSelection,x=d.selection,s=(e?e():(k)?k():(x?x.createRange().text:0)),f='http://prodougtivity.com/wp-content/plugins/linkmarklet',l=d.location,e=encodeURIComponent,u=f+'?u='+e(l.href.replace(new RegExp('(https?:\/\/)','gm'),''))+'&t='+e(d.title)+'&s='+e(s)+'&v=4&m='+(((l.href).indexOf('https://',0)===0)?1:0);a=function(){if(!w.open(u,'t','toolbar=0,resizable=1,scrollbars=1,status=1,width=720,height=570'))l.href=u;};if (/Firefox/.test(navigator.userAgent)) setTimeout(a, 0); else a();void(0)

Tried it with both Chrome & Safari unsuccessfully using your bookmarklet workflow. When running from bookmarks bar, the bookmarklet generates a new window with information pre-filled like so:

 

linkmarklet.png

Any ideas? Thanks.

Share this post


Link to post

Does it work if you remove the javascript: part? What if you run the code in the console?

 

Still does not work if I remove the javascript: portion. Chrome console references the following:

Uncaught ReferenceError: ajaxurl is not defined

That is in reference to some portion of jQuery being used in the Wordpress theme and not the bookmarklet javascript being run.

 

I can just keep running the bookmarklet from my bookmarks bar since I run everything else using your workflow. Just wasn't sure if you new of a fix. Thanks.

Share this post


Link to post

Thanks for this workflow, Vitor. Definitely one of my favorites. So far, I'm using it for squirt.io and Instapaper. I'd like to use it for Slicereader, but it doesn't seem to activate Slicereader when I try to run it. Here is what the Slicereader bookmarklet looks like:

javascript:(function%20()%20{var%20selectedText%20=%20'',title%20=%20document.title,uri%20=%20window.location.href;if%20(window.getSelection)%20{selectedText%20=%20window.getSelection().toString();}%20else%20if%20(document.getSelection)%20{selectedText%20=%20document.getSelection().toString();}%20else%20if%20(document.selection)%20{selectedText%20=%20document.selection.createRange().text;}if%20(selectedText)%20{selectedText%20=%20selectedText.replace(/(\r\n|\n+\r)/g,%20'\n');window.location.href%20=%20['slicereader://read?txt=',%20encodeURIComponent(selectedText),%20'&title=',%20encodeURIComponent(title),%20'&url=',%20encodeURIComponent(uri)].join('');}%20else%20{window.location.href%20=%20['slicereader://read?url=',%20encodeURIComponent(uri),%20'&title=',%20encodeURIComponent(title)].join('');}})();

And here is what it looks like after I run your cleaning script:

javascript:(function () {var selectedText = '',title = document.title,uri = window.location.href;if (window.getSelection) {selectedText = window.getSelection().toString();} else if (document.getSelection) {selectedText = document.getSelection().toString();} else if (document.selection) {selectedText = document.selection.createRange().text;}if (selectedText) {selectedText = selectedText.replace(/(\r\n|\n+\r)/g, '\n');window.location.href = ['slicereader://read?txt=', encodeURIComponent(selectedText), '&title=', encodeURIComponent(title), '&url=', encodeURIComponent(uri)].join('');} else {window.location.href = ['slicereader://read?url=', encodeURIComponent(uri), '&title=', encodeURIComponent(title)].join('');}})(); 

But, when I type in my keyword and press return, nothing happens (Slicereader would normally open up with the tab I run the bookmarklet in). Any ideas? I've tried deleting "javascript:" and even not cleaning it, but nothing seems to work for either browser. 

Share this post


Link to post

Thank you for the kind words, boddicker. Unfortunately, I can’t accurately debug the issue since I do not have Slicereader and don’t intend on getting it.
 
Do you see any output in Alfred’s debugger or you browser’s console, though?

Share this post


Link to post

jarhead and boddicker, are you still using those bookmarklets? Are you using Chrome?

If you are, please try changing the applescript code from tell application "Google Chrome" to tell active tab of front window to execute javascript to tell application "Google Chrome" to tell active tab of front window to set the URL to, and instead of pasting the clean bookmarklet code, paste the code before it was cleaned (with all the %20 and whatnot.

Does it work?

Share this post


Link to post

Update.

To execute bookmarklets, it now tells the browser to substitute the URL, instead of executing the javascript. Though the result is the same from the point of view of the user, I’ve always avoided doing it this way since the alternative seems cleaner. However, some bookmarklets fail with the cleaner option.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...