I make an extension that opens certain types of files with Google Docs Viewer.

I use the nsIURIContentListener to listen for specific MIME types, e.g., 
application/pdf.

I want to use a notificationBox to ask the User if they want to open that file 
with Google Docs Viewer or not.

The problem is: If the User makes an right click on the file and open that in a 
new window/tab, then the notificationBox should open in that window/tab but it 
opens in the current Tab.

What code would I need to open the notificationBox in the right window/tab?

Sorry for my bad english!

What I've managed so far: 

var gViewer = {

uriloader: 
Components.classes["@mozilla.org/uriloader;1"].getService(Components.interfaces.nsIURILoader),

  QueryInterface: function(aIID) {
     if (aIID.equals(Components.interfaces.nsIURIContentListener) ||
       aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
       aIID.equals(Components.interfaces.nsISupports))
     return this;
     
     throw Components.results.NS_NOINTERFACE;   
  },

  register: function() {

    this.uriloader.registerContentListener(this);
  },

  unregister: function() {

    this.uriloader.unRegisterContentListener(this);
  },

   getPref: function(prefName, defaultValue) {

      var prefService = Components.classes["@mozilla.org/preferences-service;1"]
                                
.getService(Components.interfaces.nsIPrefBranch);

       var prefValue = defaultValue;
       try {
         prefValue = prefService.getBoolPref(prefName);
      } catch (e) {
      }   

       return prefValue;
   },

  checkContentType: function(contentType) {

      const sPrefPrefix = "extensions.gviewer.contenthandling.files.";

      switch (contentType) {

         case "application/pdf":
            return this.getPref(sPrefPrefix + "pdf", false);
   
         case "application/vnd.ms-powerpoint":
            return this.getPref(sPrefPrefix + "ppt", false);

         case "application/msword", 
"application/vnd.openxmlformats-officedocument.wordprocessingml.document":
            return this.getPref(sPrefPrefix + "doc", false);

         default:
            return false;
      }
  },

  canHandleContent: function(contentType, isContentPreferred, 
desiredContentType) {

     return this.checkContentType(contentType);
  },

  isPreferred: function(contentType, desiredContentType) {

     return this.checkContentType(contentType);
  },

  onStartURIOpen: function(uri) {

     return false;
  },

  doContent: function(contentType, isContentPreferred, channel, contentHandler) 
{
     
    channel.QueryInterface(Components.interfaces.nsIChannel);

    // get information about the request
    var url = channel.URI.spec;
    var protocol = channel.URI.scheme; 
    var notificationCallbacks = channel.loadGroup.notificationCallbacks;

    var webNavigation = 
notificationCallbacks.getInterface(Components.interfaces.nsIWebNavigation);

    var website = webNavigation.currentURI.spec;

    if (website.indexOf("docs.google.com") != -1) {
      contentHandler = null;
        return false;
   }

    if (protocol.toLowerCase().indexOf("http") != 0) {
      contentHandler = null;
        return false;
   }
 
    var stopFlags = Components.interfaces.nsIWebNavigation.STOP_ALL;

    webNavigation.stop(stopFlags);

    var notificationBox = gBrowser.getNotificationBox();

    var notification = notificationBox.getNotificationWithValue("gDocMessage");

     var message = "Open this file with Google Docs?"

       if (notification) {
         notification.label = message;
       }
       else {
         var buttons = [{
           label: "Yes",
           accessKey: "",
           callback: function() {

         }
       },{
           label: "No",
           accessKey: "",
           callback: function() {

         }
       },{
           label: "Don't ask me again",
           accessKey: "",
           callback: function() {

         }
        }];

          const iconURL = "chrome://sendtogoogledocs/skin/question-16.png";
          const priority = notificationBox.PRIORITY_INFO_LOW;
          notificationBox.appendNotification(message, "gDocMessage",
                                             null,
                                             priority, buttons);
        }

     // handle the download
    var viewOnlineURL = "http://docs.google.com/viewer?url="; + 
encodeURIComponent(url);
   
    var loadFlags = Components.interfaces.nsIWebNavigation.LOAD_FLAGS_NONE;

    webNavigation.loadURI(viewOnlineURL, loadFlags, null, null, null);

    return true;
  }
   
};

window.addEventListener("load", function(event) { gViewer.register(event); }, 
false);
window.addEventListener("unload", function(event) { gViewer.unregister(event); 
}, false);

---
frmsrcurl: http://mozilla-xp.com/mozilla.dev.embedding
_______________________________________________
dev-embedding mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-embedding

Reply via email to