0

I'm working on an extension for Mac OS X's Safari browser. I only want this extension to execute if a few specific strings appear on the page. It should search for these strings throughout the entire HTML document.

Validation bool:

function validateStringsPresent(){
    var index1 = document.documentElement.innerHTML.indexOf(validationText1);
    var index2 = document.documentElement.innerHTML.indexOf(validationText2);

    /* For testing purposes only */
    alert("If an index = -1, it is not on the page" + "\n" + "\n"
            + index1 + "\n" + "\n"
            + index2);

    if (index1 != -1 && index2 != -1) {return true};
    else {return false};
}

As you've guessed, I get -1 for both indexes on a page that does contain the Validation Text. There are no special characters in these strings except for :, but that shouldn't be a factor.

My theory is that I need something other than the document class when using an extension in Safari. To get the current URL, for example, the document class didn't work either. I had to use var currentUrl = safari.application.activeBrowserWindow.activeTab.url;

I haven't been able to find the correct Safari Class to accomplish what I am trying to do, though.

Can anyone point me in the right direction?

EDIT

Now trying via injected script.

Button click function:

function performCommand(event)
{
    getHTML();
}

safari.application.addEventListener("command", performCommand, true);

injected.js: function getHTML()

{
    var val1 = document.documentElement.innerHTML;

    alert("You are using injected.js" + "\n" + "\n"
            + val1);    
}

As you can see in the screen shots below, getHTML() is returning the HTML from my global file and not the current web page.

Alert

Extension Builder

MrCarder
  • 438
  • 1
  • 9
  • 19
  • Where did you put this code? In the [global page](https://developer.apple.com/library/safari/documentation/Tools/Conceptual/SafariExtensionGuide/AddingaGlobalHTMLPage/AddingaGlobalHTMLPage.html) or in an [injected script](https://developer.apple.com/library/safari/documentation/Tools/Conceptual/SafariExtensionGuide/InjectingScripts/InjectingScripts.html)? – Rob W Aug 24 '13 at 21:38
  • It is in the global page and I am using a button in the toolbar to activate my extension – MrCarder Aug 24 '13 at 21:42
  • 1
    You need an injected script, and pass around a message, see https://developer.apple.com/library/safari/documentation/Tools/Conceptual/SafariExtensionGuide/MessagesandProxies/MessagesandProxies.html. – Rob W Aug 24 '13 at 21:45
  • Thanks, I am now injecting my .js file, but it is returning HTML from my global page and not the active window/tab – MrCarder Aug 24 '13 at 22:10
  • Then you're *not* injecting HTML. FYI, "Injected script" in Safari extensions is similar to "content script" in Chrome/Firefox extensions: Scripts that run in the context of a tab instead of the extension's background page. The global page is an invisible page that runs in the background. – Rob W Aug 24 '13 at 22:12
  • Do I need to build and install my extension rather than using the standard testing process? I have the .js set as an end script. I have no idea why it is grabbing my global.html file – MrCarder Aug 24 '13 at 22:14
  • I have no idea how you're running your code, so I can't comment on that unless you provide more relevant information. – Rob W Aug 24 '13 at 22:15
  • Additional details added. – MrCarder Aug 24 '13 at 22:25
  • Read the manual: https://developer.apple.com/library/safari/documentation/Tools/Conceptual/SafariExtensionGuide/MessagesandProxies/MessagesandProxies.html – Rob W Aug 24 '13 at 22:27

0 Answers0