Assign Bookmarklet to Touchbar button

Hello
I want to assign a Safari bookmarklet to a touchbar button using BTT on an M1 Macbook. Using the Real Javascript option doesn't work since, presumably, the bookmarklet code isn't real javascript. The code works 100% using the bookmarklet but zero % as a BTT button. Here's the code. Thanks in advance:

javascript:%20(function()%7Bvar%20title=encodeURIComponent(window.document.title);var%20url=encodeURIComponent(window.location.href);var%20ua=navigator.userAgent;var%20ver=%222.5%22;if%20(isM())%20mAdd();%20else%20dAdd();function%20dAdd()%7Bvar%20br=getBrowser();var%20w=579;var%20h=467;if%20(br=='firefox')%7Bif%20(isMac())%20h=462;else%20h=495;%7Delse%20if%20(br=='opera')%7Bw=600;h=554;%7Delse%20if(br=='safari')%7Bh=488;%7Delse%20if%20(br=='chrome'%20&&%20isMac())%7Bh=467;%7Delse%20if%20(br=='ie')%7Bh=452;%7Dvar%20left=(screen.width-w)/2;var%20tops=(screen.height-h)/3;win=window.open('Bookmark Ninja - Login';%7Dfunction%20isMac()%7Bif%20(ua.match(/Macintosh/i))%20return%20true;else%20return%20false;%7D%7D)();

What is this script supposed to do? It contains multiple errors.

In general you would need to use Apple Script to execute Java Script in Safari, e.g.

tell application "Safari"
    delay 0.1
    do JavaScript "document.getElementById('accountEnabledSelection').value=1" in current tab of window 1
    do JavaScript "document.getElementsByName('demo')[0].click();" in current tab of window 1


end tell

Or if you are on Monterey you can use the Shortcuts app to create such an action (which can then be called from BTT):

Thanks, Andreas.

I'm trialling a cloud bookmark facility so that my bookmarks are available across devices and different browsers. The one I'm using is Bookmark Ninja https://www.bookmarkninja.com

It's quite nice and could work for me. Anyway, the company provides extensions for browsers like Chrome, Opera, Firefox, etc but not for Safari. For the latter it only provides bookmarklets which rely on Javascript - about which I am totally ignorant. These bookmarklets sit on the bookmarks bar unlike the extensions which appear in the toolbar

The code I posted relates to one of the bookmarklets which, when clicked, will add a bookmark to my Bookmark Ninja dashboard in the tab and category I select. In other words it acts like Cmd-D that you use to add a bookmark in your local browser. It works perfectly as a bookmarklet but I have to have the bookmarks bar showing in order to use it. I'd like to hide the bar and use the touch bar to initiate the action.

It's not a big deal and I'm not that much of a real estate freak that I have to hide the bookmarks bar but, if it was easy, then I'd put it on the touch bar.

BTT is indispensable to me for so many reasons but if the coding skills to make it work with this are beyond me, which they undoubtedly are, I won't lose much sleep.

Thanks again.

Mick

Thanks Andreas.

I cracked it by throwing the javascript in the bin and achieved the same goal with a straight Apple script. It needs a bit of cosmetic stuff but works perfectly.