Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Safari extension: execute bookmarklet-url

Hey there,


I am new to Safari development and would be happy if you can help me.


I try to build a extension of a bookmarklet-url (OneNote) so I would be able to have a button in Safari – and not only a bookmark in favorites bar. But I am too silly to understand how to execute this bookmarklet-url in javascript. I tried currentTab.url but it does not work. If I click on the new shiny button nothing happens.


Which code I have to use, that the bookmarklet-url is executed – just as the link in the favorites bar works.


Link in the favorites bar:

javascript:(function(){var%20jsCode=document.createElement('script');jsCode.setAttribute('src','https://www.onenote.com/Clipper/Root?ClipperId=ON-45519f6d-f536-4649-8856-5e4157dffe10&ClipperType=Bookmarklet&ClipperVersion=2.0.2');jsCode.setAttribute('id','oneNoteCaptureRootScript');jsCode.setAttribute('type','text/javascript');document.body.appendChild(jsCode);})()


global.html

<!DOCTYPE html>
<html>
  <head>
  <title>Microsoft OneNote Clipper</title>
  <script type="text/javascript" charset="utf-8">
  // register with application
  safari.application.addEventListener("command", performCommand, false);

  function performCommand(event) {
  if (event.command === "clip-onenote") {
  // Run OneNote Clipper
  (function(){
  var jsCode = document.createElement('script');
  jsCode.setAttribute('src','https://www.onenote.com/Clipper/Root?ClipperId=ON-45519f6d-f536-4649-8856-5e4157dffe10&ClipperType=Bookmarklet&ClipperVersion=2.0.2');
  jsCode.setAttribute('id','oneNoteCaptureRootScript');
  jsCode.setAttribute('type','text/javascript');
  document.body.appendChild(jsCode);
  })()
  }
  }
  </script>
  </head>
  <body>
  </body>
</html>


Thank you very much for your help! 🙂

MacBook Air (13-inch Mid 2012), OS X Yosemite (10.10.3), Safari

Posted on May 27, 2015 11:38 AM

Reply
3 replies

May 28, 2015 9:29 AM in response to rapid10

Hello rapid10,

Have you seen Apple's documentation?

https://developer.apple.com/library/safari/documentation/Tools/Conceptual/Safari ExtensionGuide/UsingExtensionBuilder/UsingExtensionBuilder.html


I haven't written an extension that added any buttons in the window frame or anything. I have added buttons into a page. What you have posted really doesn't look like a valid Safari extension. The global page is for settings and message handling. Most of the work happens in your injected script.

May 28, 2015 11:37 AM in response to rapid10

Hello again rapid10,

I double-checked and it isn't so simple. I suggest you work on creating an extension that just displays a button that, when clicked, will show the URL for the current page. This will require some complex back and forth messaging between the global page and an inject script. It probably isn't worth your effort.

Safari extension: execute bookmarklet-url

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.