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.