[or-cvs] [torbutton/master] Added to context menu for links that includes 2 options: "Open Link as Tor URL" and "Copy link as Tor URL"

mikeperry at seul.org mikeperry at seul.org
Wed Sep 23 23:49:08 UTC 2009


Author: Kory Kork <kory at korykirk.com>
Date: Sun, 19 Jul 2009 17:03:34 +0000
Subject: Added to context menu for links that includes 2 options:
Commit: 2ee919b1e9b4b7755e705392fddf1c7980c38aa8

svn:r20089
---
 src/chrome/content/torbutton.js  |   71 +++++++++++++++++++++++++++++++++++++-
 src/chrome/content/torbutton.xul |   14 +++++++
 2 files changed, 84 insertions(+), 1 deletions(-)

diff --git a/src/chrome/content/torbutton.js b/src/chrome/content/torbutton.js
index cca3bf7..8c1dfd6 100644
--- a/src/chrome/content/torbutton.js
+++ b/src/chrome/content/torbutton.js
@@ -97,7 +97,7 @@ var torbutton_unique_pref_observer =
                     || !m_tb_prefs.prefHasUserValue("general.useragent.vendorSub")) 
                     && m_tb_prefs.getBoolPref("extensions.torbutton.tor_enabled")
                     && m_tb_prefs.getBoolPref("extensions.torbutton.set_uagent")) {
-                    torbutton_log(4, "Some other addon tried to clear user agent settings.");
+                    torbutton_log(4, "Some other addond tried to clear user agent settings.");
                     torbutton_set_uagent();
                 }
                 break;
@@ -424,6 +424,12 @@ function torbutton_init() {
         torbutton_log(1, 'skipping pref observer init');
     }
     
+    //setting up context menu
+    var contextMenu = document.getElementById("contentAreaContextMenu");
+    if (contextMenu)
+      contextMenu.addEventListener("popupshowing", torbutton_check_contextmenu, false);
+    
+    
     torbutton_set_panel_view();
     torbutton_log(1, 'setting torbutton status from proxy prefs');
     torbutton_set_status();
@@ -433,6 +439,69 @@ function torbutton_init() {
     torbutton_log(3, 'init completed');
 }
 
+//this function checks to see if the context menu is being clicked on a link.
+//if it is, then we show the two context menu items
+function torbutton_check_contextmenu() {
+    var torurl = document.getElementById("torcontext-copyurl");
+    var tortab = document.getElementById("torcontext-opentorurl");
+    torurl.hidden = tortab.hidden = (document.popupNode.localName != "A") 
+}
+function torbutton_copy_link() {
+  var element = document.popupNode;
+  var myURI = Components.classes["@mozilla.org/network/io-service;1"]
+                      .getService(Components.interfaces.nsIIOService)
+                      .newURI(element, null, null);
+  //check the scheme
+  if (myURI.scheme == "http" || myURI.scheme == "tor")
+    myURI.scheme = "tor";
+  else if (myURI.scheme == "https" || myURI.scheme == "tors")
+    myURI.scheme = "tors";
+    else
+      return;//unsupported scheme      
+  torbutton_copyToClipboard(myURI.spec);  
+}
+function torbutton_copyToClipboard(copyThis) {	
+	
+	var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
+	if (!str) return false;
+	str.data = copyThis; // unicode string?
+	
+	var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
+	if (!trans) return false; //no transferable widget found
+	
+	trans.addDataFlavor("text/unicode");
+	trans.setTransferData("text/unicode", str, copyThis.length*2); // *2 cuz it's unicode
+	
+	var clipid=Components.interfaces.nsIClipboard;
+	var clip = Components.classes["@mozilla.org/widget/clipboard;1"].getService(clipid);
+	if (!clip) return false; // couldn't get the clipboard
+	
+	clip.setData(trans, null, clipid.kGlobalClipboard);
+	return true;
+}
+//opens new tab with link with tor:// protocol
+function torbutton_open_link_as_tor() {
+  var element = document.popupNode;
+  var mainWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
+                   .getInterface(Components.interfaces.nsIWebNavigation)
+                   .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
+                   .rootTreeItem
+                   .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
+                   .getInterface(Components.interfaces.nsIDOMWindow);
+  //make into nsIURI
+  var myURI = Components.classes["@mozilla.org/network/io-service;1"]
+                      .getService(Components.interfaces.nsIIOService)
+                      .newURI(element, null, null);
+  //check the scheme
+  if (myURI.scheme == "http" || myURI.scheme == "tor")
+    myURI.scheme = "tor";
+  else if (myURI.scheme == "https" || myURI.scheme == "tors")
+    myURI.scheme = "tors";
+    else
+      return;//unsupported scheme    
+  mainWindow.getBrowser().addTab(myURI.spec);    
+}
+
 // this function duplicates a lot of code in preferences.js for deciding our
 // recommended settings.  figure out a way to eliminate the redundancy.
 // TODO: Move it to torbutton_util.js?
diff --git a/src/chrome/content/torbutton.xul b/src/chrome/content/torbutton.xul
index 3809325..e339944 100644
--- a/src/chrome/content/torbutton.xul
+++ b/src/chrome/content/torbutton.xul
@@ -45,4 +45,18 @@
         <popup id="torbutton-context-menu"/>
 <!--        <statusbarpanel context="clipmenu" class="statusbarpanel-iconic" id="plugins-status" tooltip="plugin-tooltip" onclick="torbutton_toggle_plugins();"/> -->
    </statusbar>
+   <popup id="contentAreaContextMenu">
+			<menuitem 
+					id="torcontext-copyurl"
+					label="Copy Tor URL" 
+					accesskey="t"
+					insertafter="context-copylink"
+					oncommand="torbutton_copy_link();"/>  
+			    <menuitem 
+					id="torcontext-opentorurl"
+					label="Open Link as Tor URL" 
+					accesskey="r" 
+					insertafter="context-openlinkintab"					
+					oncommand="torbutton_open_link_as_tor();"/>
+  </popup>
 </overlay>
-- 
1.5.6.5




More information about the tor-commits mailing list