Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var _ = require("sdk/l10n").get;
- const tabs = require("sdk/tabs");
- const {viewFor} = require('sdk/view/core');
- const {modelFor} = require('sdk/model/core');
- const {getBrowserForTab, getTabBrowserForTab, getTabForContentWindow} = require("sdk/tabs/utils");
- const {Ci, Cu} = require("chrome");
- Cu.import("resource://gre/modules/XPCOMUtils.jsm", this);
- var youtubePattern = /https?:\/\/www\.youtube\.com\/watch\?v=(\S+)/;
- var ns = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
- var youtubeURLListener = {
- oldURL: null,
- matchURL: function(newURL) {
- if (newURL == this.oldURL) return -1;
- this.oldURL = newURL;
- return newURL.match(youtubePattern);
- },
- QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebProgressListener, Ci.nsISupportsWeakReference]),
- onLocationChange: function(aBrowser, aProgress, aRequest, aURI) {
- var highLevelTab = modelFor(getTabForContentWindow(aProgress.DOMWindow));
- var urlMatch = this.matchURL(highLevelTab.url);
- if (urlMatch != -1) {
- var ytId = urlMatch[1].substring(0,11); //YT doesn't guarantee that IDs will stay 11 char
- var doc = viewFor(highLevelTab.window).document;
- var tabContextMenu = doc.getElementById("tabContextMenu");
- console.log(ytId, highLevelTab.id);
- var sep = doc.createElementNS(ns,"menuseparator");
- var hbox = doc.createElementNS(ns, "hbox");
- hbox.setAttribute("id", "contexttab-controlsBox");
- hbox.setAttribute("height", "24");
- var playButton = doc.createElementNS(ns,"button");
- playButton.setAttribute("id", "contexttab-newtab");
- playButton.setAttribute("label", _("newtab_string"));
- playButton.setAttribute("accesskey", _("newtabaccesskey_string"));
- playButton.setAttribute("oncommand", "BrowserOpenTab();");
- playButton.setAttribute("icon", "./icon-16.png");
- hbox.appendChild(playButton);
- tabContextMenu.insertBefore(sep, tabContextMenu.firstChild);
- tabContextMenu.insertBefore(hbox, tabContextMenu.firstChild);
- highLevelTab.attach({
- contentScriptFile: "./yt-controls.js"
- });
- worker.port.emit("ytId", ytId);
- }
- }
- };
- let gBrowser = getTabBrowserForTab(viewFor(tabs[0]));
- gBrowser.addTabsProgressListener(youtubeURLListener);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement