Bookmarklet that additionally closes tab?

I have a bookmarklet that creates bookmarks in DTTG (in order to capture them as PDF on the mac).

javascript:window.location='x-devonthink://createBookmark?title='+encodeURIComponent(document.title)+'&location='+encodeURIComponent(window.location)+'&destination=04CDA21B-FFD8-41A3-9798-B00AC1C09B9A';

I’m now trying to automatically close the tab in Safari, i.e. let the bookmarklet create the bookmark in DTTG and close the tab in Safari. Is this possible?

Already tried to add ;window.close(); - this closes the tab but doesn’t create the bookmark in DTTG.

(I know I could use the Safari button at the upper left to go back, but that’s very uncomfortable for my fingers, even on a small iPhone SE. So instead I’m switching apps via the home button. I’m doing this when I’m processing Twitter in Tweetbot (which I’ve set to open links in Safari), which means I’m currently going from Tweetbot > Safari > DTTG > Safari > Tweetbot)

Probably a race condition. Although I’m not sure that window.close works with tabs.
See here: javascript - How to close current tab in a browser window? - Stack Overflow

1 Like

Getting a link found in a tweet in Tweetbot into a DTTG group was cumbersome.

I had to

  1. tap link in Tweetbot
  2. tap adress bar in Safari
  3. tap bookmarklet
  4. confirm alert
  5. double tab home button
  6. tap on Safari
  7. tap tabs button
  8. close tab
  9. double tab home button
  10. swipe to see Tweetbot
  11. tap on Tweetbot

This was very annoying. I often tried to streamline it but always gave up.

Thanks to @chrillek who pointed me to setTimeout() I’ve now found a much faster way.

This bookmarklet

  • closes the current tab
  • creates a bookmark in DTTG
  • switches to Tweetbot

javascript:window.setTimeout(() => {window.close()}, 5000);window.location='x-devonthink://x-callback-url/createBookmark?title='+encodeURIComponent(document.title)+'&location='+encodeURIComponent(window.location)+'&destination=04CDA21B-FFD8-41A3-9798-B00AC1C09B9A&x-success=tweetbot://';

Now there are only 4 taps needed

  1. tap link in Tweetbot
  2. tap adress bar in Safari
  3. tap bookmarklet
  4. confirm alert

What a difference. Thanks a lot chrillek! :grinning:

You’re welcome. I’m not sure if maybe you could use encodeURI for the whole location, saving some calls to encodeURIComponent (and making the code a bit shorter).
In that case,

window.location=encodeURI(`x-devonthink://x-callback-url/createBookmark?title=${document.title}&location=${window.location}&destination=04CDA21B-FFD8-41A3-9798-B00AC1C09B9A&x-success=tweetbot://`

would be a bit “nicer”. The enclosing backticks are used for “template strings”, in which you can expand JavaScript expressions by including them in ${...}. Which should make them more readable than a long sequence of 'a'+'b'. In theory, at least.

A screencast of this could be useful.

I’ll try that but at the moment I’m glad that it works at all :slight_smile:

  1. Tap a link in Tweetbot

  1. Tab adress bar in Safari

  1. Tap bookmarklet

  1. Confirm alert

  1. Automatically back in Tweetbot

That’s it

1 Like

Very nice. I think that will be helpful for folks to visualize the process better.
Thanks.

1 Like