Search code examples
javascriptbrowserautohotkeybookmarklet

Running A Bookmarklet With Autohotkey


I have this neat little bookmarklet that refreshes the stylesheet on whatever webpage you are looking at without reloading the page:

javascript:(function(){var h,a,f;a=document.getElementsByTagName('link');for(h=0;h<a.length;h++){f=a[h];if(f.rel.toLowerCase().match(/stylesheet/)&&f.href){var g=f.href.replace(/(&|%5C?)forceReload=\d+/,'');f.href=g+(g.match(/\?/)?'&':'?')+'forceReload='+(new Date().valueOf())}}})()

I'm trying to write an AHK script to run the bookmarklet when I hit CTRL + Q. This is what I've got so far:

^q::
Clipboard:="?javascript:(function(){var h,a,f;a=document.getElementsByTagName('link');for(h=0;h<a.length;h++){f=a[h];if(f.rel.toLowerCase().match(/stylesheet/)&&f.href){var g=f.href.replace(/(&|%5C?)forceReload=\d+/,'');f.href=g+(g.match(/\?/)?'&':'?')+'forceReload='+(new Date().valueOf())}}})()"
sendinput ^l
sendinput {Right}
sendinput ^v
sendinput {enter}
return

It doesn't help that I don't know how to execute the bookmarklet without actually clicking it in the bookmark bar. The above codes selects the URL and pastes the javascript at the end of it - but this doesn't work.

Can anyone help me out?

EDIT: here is another attempt but this just searches google for the actual string instead of running it.

^q::
Clipboard:="javascript:(function(){var h,a,f;a=document.getElementsByTagName('link');for(h=0;h<a.length;h++){f=a[h];if(f.rel.toLowerCase().match(/stylesheet/)&&f.href){var g=f.href.replace(/(&|%5C?)forceReload=\d+/,'');f.href=g+(g.match(/\?/)?'&':'?')+'forceReload='+(new Date().valueOf())}}})()"
sendinput ^l
sendinput ^v
sendinput {enter}
return

Solution

  • By doing a bit of research, this may well work as a workaround:

    1. Add a custom search engine in Chrome by right clicking the address bar, and clicking add next to "other search engines"
    2. Name it something sensible and give it a keyword (in this case I used "refreshcss")
    3. Paste the bookmarklet in the URL field and save.

    Now modify the AHK script as follows:

    ^q::
    sendinput ^l
    send refreshcss
    sendinput {enter}
    return
    

    This will take you to the URL bar, select your bookmarklet and run it :)