Advertisement
Guest User

Untitled

a guest
Feb 25th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. var _ = require("sdk/l10n").get;
  2. const tabs = require("sdk/tabs");
  3. const {viewFor} = require('sdk/view/core');
  4. const {modelFor} = require('sdk/model/core');
  5. const {getBrowserForTab, getTabBrowserForTab, getTabForContentWindow} = require("sdk/tabs/utils");
  6. const {Ci, Cu} = require("chrome");
  7. Cu.import("resource://gre/modules/XPCOMUtils.jsm", this);
  8.  
  9. var youtubePattern = /https?:\/\/www\.youtube\.com\/watch\?v=(\S+)/;
  10. var ns = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  11.  
  12. var youtubeURLListener = {
  13. oldURL: null,
  14.  
  15. matchURL: function(newURL) {
  16. if (newURL == this.oldURL) return -1;
  17.  
  18. this.oldURL = newURL;
  19. return newURL.match(youtubePattern);
  20. },
  21.  
  22. QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebProgressListener, Ci.nsISupportsWeakReference]),
  23. onLocationChange: function(aBrowser, aProgress, aRequest, aURI) {
  24. var highLevelTab = modelFor(getTabForContentWindow(aProgress.DOMWindow));
  25. var urlMatch = this.matchURL(highLevelTab.url);
  26. if (urlMatch != -1) {
  27. var ytId = urlMatch[1].substring(0,11); //YT doesn't guarantee that IDs will stay 11 char
  28. var doc = viewFor(highLevelTab.window).document;
  29. var tabContextMenu = doc.getElementById("tabContextMenu");
  30.  
  31. console.log(ytId, highLevelTab.id);
  32.  
  33. var sep = doc.createElementNS(ns,"menuseparator");
  34. var hbox = doc.createElementNS(ns, "hbox");
  35. hbox.setAttribute("id", "contexttab-controlsBox");
  36. hbox.setAttribute("height", "24");
  37.  
  38. var playButton = doc.createElementNS(ns,"button");
  39. playButton.setAttribute("id", "contexttab-newtab");
  40. playButton.setAttribute("label", _("newtab_string"));
  41. playButton.setAttribute("accesskey", _("newtabaccesskey_string"));
  42. playButton.setAttribute("oncommand", "BrowserOpenTab();");
  43. playButton.setAttribute("icon", "./icon-16.png");
  44. hbox.appendChild(playButton);
  45.  
  46. tabContextMenu.insertBefore(sep, tabContextMenu.firstChild);
  47. tabContextMenu.insertBefore(hbox, tabContextMenu.firstChild);
  48.  
  49. highLevelTab.attach({
  50. contentScriptFile: "./yt-controls.js"
  51. });
  52. worker.port.emit("ytId", ytId);
  53. }
  54. }
  55. };
  56.  
  57. let gBrowser = getTabBrowserForTab(viewFor(tabs[0]));
  58. gBrowser.addTabsProgressListener(youtubeURLListener);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement