Commits

chaals committed 79ed162

Bumped version.
Cleaned code a bit.
Don't let pages loading in background interfere.

Comments (0)

Files changed (4)

 <?xml version="1.0" encoding="utf-8"?>
-<widget xmlns="http://www.w3.org/ns/widgets" version="1.2"
+<widget xmlns="http://www.w3.org/ns/widgets" version="1.3"
   defaultlocale="en">
   <name>Swaplang</name>
   <author href="http://my.opera.com/chaals">Chaals</author>
 	<description xml:lang="es">Elegir entre los idiomas disponible para la pagina</description>
 	<description xml:lang="fr">Choisir entre les langues dans lesquelles une page est disponible</description>
         <description xml:lang="de">Wählen Sie zwischen den Sprache, die für die Seite verfügbar sind</description>
-	<description xml:lang="">Swap between different page languages</description>
   <license href="http://www.apache.org/licenses/LICENSE-2.0">Apache 2.0. See the file license.txt in this extension package for details</license>
 </widget>

includes/swaplang-userjs.js

-var channel;
-
-// Funtion to see if some item is in a list
-// Used to weed out duplicate links
-function memberOf(item,list) {
-  var a;
-  for (a=0;a<list.length;a++)
-    if (item == list[a]) return true;
-  return false;
-}
-
-window.addEventListener("load", function(){
-
-  //------------------------------------
+window.addEventListener("DOMContentLoaded", function(){
+
   //If there are language choices available,
   //create a simpler object containing just the data we need
 
 
   if (languageChoices.length) {
     for( i in languageChoices){
-      if (memberOf(languageChoices[i].href,linksList) || !(languageChoices[i].href)) continue;
+      if ((linksList.indexOf(languageChoices[i].href) > -1) || !(languageChoices[i].href)) continue;
       tempObj[i] = {};
       tempObj[i].href = languageChoices[i].href;
       linksList.push(languageChoices[i].href);
     for (i in tempObj)
       if (tempObj[i].href) listLength++;
   }
-  //------------------------------------
-  //If the tab is already focused, the lang choices must be manually reported.
-  reportLangChoices();
-  //------------------------------------
+
+  //Tell background to request choices from focused tab
+  opera.extension.postMessage("ready");
   
-  
-  //------------------------------------
-  //Method which checks if there are language choices,
-  //And tells the background if there are.
+  //If there are language choices, tell the background
   function reportLangChoices(){
-    if (listLength) {
-      opera.extension.postMessage(("languagesAvailable: " + listLength));
-      opera.extension.postMessage(("longest: " + longString));
+    if (listLength) {
+      theInfo = {
+	"languagesAvailable"	: listLength,
+	"longestString"		: longString.length
+      }
+      opera.extension.postMessage(JSON.stringify(theInfo));
     } else
       opera.extension.postMessage("none");
   }
-  //------------------------------------
-  
-  
-  //------------------------------------
-  //
-  function setupChannel( shortTermPort ){
-    channel = new MessageChannel();
-    channel.port1.onmessage = function( event ){
+
+  //Pass the choices to the popup through a forwarded port
+  function setupChannel(shortTermPort) {
+    var channel = new MessageChannel();
+    channel.port1.onmessage = function(event) {
       window.location.href = event.data;
     }
-    shortTermPort.postMessage( JSON.stringify(tempObj), [channel.port2] );
+    shortTermPort.postMessage(JSON.stringify(tempObj), [channel.port2]);
   }
   //------------------------------------
   
     
   //------------------------------------
   //Handle onmessage requests
-  opera.extension.onmessage = function( event ){
-    switch( event.data ){
+  opera.extension.onmessage = function(event) {
+    switch(event.data) {
       case "reportLangChoices":
         reportLangChoices();
       break;
       case "setupChannel":
-        setupChannel( event.ports[0] );
+        setupChannel(event.ports[0]);
       break;
     }
   }
 
   var theSize = document.getElementById("item").currentStyle["fontSize"];
   var fsize = parseInt(theSize.substring(0,theSize.indexOf('px')));
-  //------------------------------------
-  // Create a disabled button in Opera
+
   var UIItemProperties = {
     disabled: true,
     title: "Change Languages",
   theButton = opera.contexts.toolbar.createItem(UIItemProperties);
   opera.contexts.toolbar.addItem(theButton);
 
-  
   function enableButton(){
-    theButton.disabled = false;
     theButton.icon = "icon.png";
-    theButton.disabled = true;
     theButton.disabled = false;
   }
   function disableButton(){
+    theButton.disabled = true;
     theButton.icon = "icno.png";
-    theButton.disabled = true;
   }
-  //------------------------------------
-  
-  
-  
-  //------------------------------------
+
   //Always disable the button on blur or loading a new page
   opera.extension.tabs.onblur = disableButton;
 
-
   //Onfocus, demand that the userJS report if there are language choices
-  opera.extension.tabs.onfocus = opera.extension.windows.onfocus = function( event ){
+  opera.extension.tabs.onfocus = opera.extension.windows.onfocus = requestChoices;
+
+  function requestChoices() {
     var currentTab = opera.extension.tabs.getFocused();
-    if ( currentTab ) {
-      currentTab.postMessage( "reportLangChoices" );
+    if (currentTab) {
+      currentTab.postMessage("reportLangChoices");
     }
   }
-  //------------------------------------
-  
-  
   
   //------------------------------------
   opera.extension.onmessage = function( event ){
     // Messages are received by the background only from the userJS, and only when language selectors are found.
     // If there are languages enable the button, set the height.
     // Set the popup width to a size based on the longest string
-    // @@TODO: That should really be the offsetWidth from popup
-    if (event.data.substring(0,9) == "languages") {
+    // @@TODO: That should really be the offsetWidth from popup
+    if (event.data == "none") 
+      disableButton();
+    else if (event.data == "ready") 
+      requestChoices();
+    else {
+      var theInfo = JSON.parse(event.data);
+      theButton.popup.height = (theInfo.languagesAvailable * 1.2 + 2) * fsize;
+      theButton.popup.width = (theInfo.longestString + 2) * fsize / 2;
       enableButton();
-      var tempSize = parseInt(event.data.substring(20));
-      theButton.popup.height = (tempSize * 1.2 + 2) * fsize;
-    } else if (event.data.substring(0,7) == "longest") {
-      theButton.popup.width = (event.data.length + 2) * fsize / 2;
-    } else if (event.data == "none")
-      disableButton();
+    }
   }
-  //------------------------------------
-  
-  
-  //------------------------------------
-  //Check if a popup connects, and there is a focused active tab.
-  // --> Forward the event.source
+
+  //If a popup connects, and there is a focused active tab,
+  // forward a port to get the menu
   opera.extension.onconnect = function( event ){
     var currentTab = opera.extension.tabs.getFocused();
     if (currentTab && event.origin.indexOf("popup.html") > -1 && event.origin.indexOf('widget://') > -1) {
   var portToUserJS;
   //------------------------------------
   //Direct messages are always from the userJS.
-  opera.extension.onmessage = function( event ){
+  opera.extension.onmessage = function(event) {
     //The port to communicate on
       portToUserJS = event.ports[0];
 
     
     //Split the JSON data, and create the links
-      var languageChoices = JSON.parse( event.data );
+      var languageChoices = JSON.parse(event.data);
       var theList = document.getElementById('list');
-      for (i in languageChoices) {
+      for (i in languageChoices) {
+opera.postError(("processing item " + i));
       //JSON data has a length and item attribute, which has to be ignored.
         if(i == "length" || i == "item") continue;