fivedogit
fivedogit

Reputation: 8524

Are these 2 capabilities supported in Safari extension development?

I've written a Chrome/Opera extension and am considering porting it to Safari. It needs 2 specific capabilities and the absence of either would veto the whole plan.

  1. Ability to read HTTPS urls - Chrome supports this as part of the "tabs" permission. Firefox (last I checked) does not.

  2. Ability to dynamically change the appearance of the activation button. - In Chrome, this is achieved by having a single canvas on the background page (i.e. the button)...

    -body- -canvas id="button_canvas" width="19" height="19"- -/canvas- -/body-

... and then dynamically changing it whenever necessary ...

var canvas = document.getElementById("button_canvas");
var context = canvas.getContext("2d");
var imageData = context.getImageData(0, 0, 19, 19);
// write stuff to the canvas
context.putImageData(imageData, 0, 0);
imageData = context.getImageData(0, 0, 19, 19);
// key element below
chrome.browserAction.setIcon({
   imageData: imageData
});

If anyone can answer these two questions definitively before I buy a used Mac on Craigslist, I'd appreciate it. Thanks!

p.s. FWIW, my own Googling suggests that #2 is not possible in Safari. No idea about #1.

Upvotes: 1

Views: 157

Answers (1)

Matt Swain
Matt Swain

Reputation: 3887

I believe both are possible.

For HTTPS URLs: In the Safari extension builder, under Extension Website Access, set Access Level to All and tick the Include Secure Pages checkbox.

To dynamically change the icon displayed on a toolbar icon, first set any icon in the Safari extension builder. Then if you want to change in response to a toolbar button click:

safari.application.addEventListener('command', performCommand, false);

function performCommand(event) {
    if (event.command === 'changeIcon') {
        event.target.image = safari.extension.baseURI+'othericon.png';
    }
}

Under other circumstances, you can iterate your toolbar buttons to modify the one you want:

var toolbarButtons = safari.extension.toolbarItems;
for (var i = 0; i < itemArray.length; ++i) {
    var item = toolbarButtons[i];
    if (item.identifier === "mybutton") {
        item.image = safari.extension.baseURI+'othericon.png';
    }
}

Upvotes: 2

Related Questions