Skip to content

Commit

Permalink
update main.js to create navButton
Browse files Browse the repository at this point in the history
  • Loading branch information
constanton committed Nov 29, 2012
1 parent e90b103 commit f3d8ab2
Showing 1 changed file with 40 additions and 7 deletions.
47 changes: 40 additions & 7 deletions lib/main.js
@@ -1,43 +1,76 @@
//Initialize everything we need
var tabs = require("tabs");
var windows = require("windows").browserWindows;
var widgets = require("widget");
var self = require("self");
data = self.data;
var { MatchPattern } = require("match-pattern");
var {Cc, Ci} = require("chrome");
var mediator = Cc['@mozilla.org/appshell/window-mediator;1'].getService(Ci.nsIWindowMediator);

//variable to store the script (string) to run when a button or widget is clicked
var myScript;

exports.main = function() {

//call the function down below to add a button to the navigation bar
addToolbarButton();

var myScript;

//when the tab is ready check the URL and store the proper value to myScript
tabs.on('ready', function(tab) {
var tabURL = tab.url;

var pattern = new MatchPattern(/.*youtube\.com\/watch.*/);

var pattern2 = new MatchPattern(/.*&html5=1*/);

//if URL is correct create myScript
if ((pattern.test(tab.url)) && !(pattern2.test(tab.url))) {
var stringarray = ["window.location.href=\"", tabURL, "&html5=1\"" ]
myScript = stringarray.join('') ;
}else{
}else{
//if URL not correct do nothing
myScript = null;
}
});


//create a clickable widget that runs myScript
var widget = widgets.Widget({

id: "switch",
label: "Switch player",
contentURL: data.url("icon.html"),



onClick: function() {
tabs.activeTab.attach({
contentScript: myScript
});
});
}

});
};


//function to create the button on the navigation bar
function addToolbarButton() {
var document = mediator.getMostRecentWindow("navigator:browser").document;
var navBar = document.getElementById("nav-bar");
if (!navBar) {
return;
}
var navButton = document.createElement("toolbarbutton");

navButton.setAttribute('type', 'button');
navButton.setAttribute('class', 'toolbarbutton-1');
navButton.setAttribute('image', data.url('yhs.ico'));
navButton.setAttribute('orient', 'horizontal');
navButton.setAttribute('label', 'YouTube HTML Switch');
navButton.addEventListener('click', function() {
tabs.activeTab.attach({
contentScript: myScript
});
}, false)
navBar.appendChild(btn);
}


0 comments on commit f3d8ab2

Please sign in to comment.