Jump to content

Trigger javascript with a hotkey


Recommended Posts

Hi, I am trying to trigger the instapaperizing action from Alfred.

I seted up a hotkey, followed by either a run script action or an open page in browser, with this code inside, but nothing seams to work… any clue?

 

Thanks!

 

 

javascript:function%20iptxt()%7Bvar%20d%3Ddocument%3Btry%7Bif(!d.body)throw(0)%3Bwindow.location%3D%27http://www.instapaper.com/text%3Fu%3D%27%2BencodeURIComponent(d.location.href)%3B%7Dcatch(e)%7Balert(%27Please%20wait%20until%20the%20page%20has%20loaded.%27)%3B%7D%7Diptxt()%3Bvoid(0)
 

 

Link to comment

Hi, I am trying to trigger the instapaperizing action from Alfred.

I seted up a hotkey, followed by either a run script action or an open page in browser, with this code inside, but nothing seams to work… any clue?

 

Thanks!

 

 

javascript:function%20iptxt()%7Bvar%20d%3Ddocument%3Btry%7Bif(!d.body)throw(0)%3Bwindow.location%3D%27http://www.instapaper.com/text%3Fu%3D%27%2BencodeURIComponent(d.location.href)%3B%7Dcatch(e)%7Balert(%27Please%20wait%20until%20the%20page%20has%20loaded.%27)%3B%7D%7Diptxt()%3Bvoid(0)
 

 

I'm assuming that you are just trying to "open" this? I would assume that OSX probably doesn't know how to handle that because javascript exists in the browser and not OS-wide. You can still do it though.

 

Try this instead.

tell application "Safari" to do JavaScript "javascript:function iptxt(){var d=document;try{if(!d.body)throw(0);window.location='http://www.instapaper.com/text?u='+encodeURIComponent(d.location.href);}catch(e){alert('Please wait until the page has loaded.');}}iptxt();void(0)" in the current tab of the front window

 

That triggers the javascript to run in the current tab of the front window in Safari (assuming that's what your using). We may have to tweak it a little bit if you aren't using Safari but I would think it would still be possible in the other browsers.

Link to comment
  • 8 years later...

The link expired. How can I trigger a javascript on Alfred to run it on Safari at the same page?

 

javascript:(function()%7B(function%20()%20%7Bvar%20i%2C%20elements%20%3D%20document.querySelectorAll('body%20*')%3Bfor%20(i%20%3D%200%3B%20i%20%3C%20elements.length%3B%20i%2B%2B)%20%7Bif%20(getComputedStyle(elements%5Bi%5D).position%20%3D%3D%3D%20'fixed')%20%7Belements%5Bi%5D.parentNode.removeChild(elements%5Bi%5D)%3B%7D%7D%7D)()%7D)()

 

Link to comment
8 hours ago, Saez said:

The link expired. How can I trigger a javascript on Alfred to run it on Safari at the same page?
 


javascript:(function()%7B(function%20()%20%7Bvar%20i%2C%20elements%20%3D%20document.querySelectorAll('body%20*')%3Bfor%20(i%20%3D%200%3B%20i%20%3C%20elements.length%3B%20i%2B%2B)%20%7Bif%20(getComputedStyle(elements%5Bi%5D).position%20%3D%3D%3D%20'fixed')%20%7Belements%5Bi%5D.parentNode.removeChild(elements%5Bi%5D)%3B%7D%7D%7D)()%7D)()

 

 

As described above:

 

 

Link to comment

O try to follow it but, I'm sorry, I couldn't get success. Step-by-step I tap to add a new one > Template > Essentials > Keyword to Script.
In fact, I will add a hotkey to run on Safari "Control+Option+Command+K" in any website but it's not working.

Link to comment

You have it set to the wrong language. That’s AppleScript, so you need Language = /usr/bin/osascript (AS)

 

You also got the AppleScript wrong. It’s tell application "Safari" to do JavaScript "<JS code goes here>" in the current tab of the front window (like in the post I linked you to.)

 

Finally, there’s actually no need to use javascript: and URL-encode the JavaScript. You’re telling the browser to execute code, not open a URL.

 

I'd provide you with a working version, but I'm afraid I don't understand what the JS is supposed to do or where.

Link to comment

You can paste that code as-is into your AppleScript:

 

tell application "Safari" to do JavaScript "(function () { 
  var i, elements = document.querySelectorAll('body *');

  for (i = 0; i < elements.length; i++) {
    if (getComputedStyle(elements[i]).position === 'fixed') {
      elements[i].parentNode.removeChild(elements[i]);
    }
  }
})();" in the current tab of the front window

 

A slightly shorter version that does the same thing:

 

tell application "Safari" to do JavaScript "(function() {document.querySelectorAll('body *').forEach(el => { if (getComputedStyle(el).position === 'fixed') el.remove(); }) })();" in the current tab of the front window

 

Edited by deanishe
Link to comment

Forget it friend! I found the error on the log
 

Quote

[11:46:41.831] ERROR: Kill Sticky[Run Script] /Users/gustavosaez/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts/C01C00E4-0A67-4A64-962F-376AB4B122A8:29:332: execution error: Safari got an error: You must enable the 'Allow JavaScript from Apple Events' option in Safari's Develop menu to use 'do JavaScript'. (8)


Now, allowed, it worked. Thank you so much!!!

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...