Commits

chaals committed 61c7599

first file commit

Comments (0)

Files changed (12)

+<?xml version="1.0" encoding="utf-8"?>
+<widget xmlns="http://www.w3.org/ns/widgets" version="1.0">
+	<name>Swaplang</name>
+        <author href="http://my.opera.com/chaals">Chaals</author>
+        <icon src="ic.png" width="64" height="64"/>
+	<description>Swap between different page languages</description>
+</widget>
Added
New image

icno.png

Added
New image

icon.png

Added
New image

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(){
+
+  //------------------------------------
+  //If there are language choices available,
+  //create a simpler object containing just the data we need
+
+  var languageChoices = document.querySelectorAll('link[hreflang][rel=alternate],a[hreflang][rel=alternate]');
+  var tempObj = {};
+  var listLength = 0;
+  var linksList = [];
+  var longString = "Also available";
+
+  if (languageChoices.length) {
+    for( i in languageChoices){
+      if (memberOf(languageChoices[i].href,linksList) || !(languageChoices[i].href)) continue;
+      tempObj[i] = {};
+      tempObj[i].href = languageChoices[i].href;
+      linksList.push(languageChoices[i].href);
+      if (languageChoices[i].tagName == "LINK")
+        tempObj[i].title = languageChoices[i].title;
+      else
+        tempObj[i].title = languageChoices[i].innerText;
+      longString = (longString.length > tempObj[i].title.length) ? longString : tempObj[i].title;
+      tempObj[i].hreflang = languageChoices[i].hreflang;
+    }
+    for (i in tempObj)
+      if (tempObj[i].href) listLength++;
+  }
+  //------------------------------------
+  //If the tab is already focused, the lang choices must be manually reported.
+  reportLangChoices();
+  //------------------------------------
+  
+  
+  //------------------------------------
+  //Method which checks if there are language choices,
+  //And tells the background if there are.
+  function reportLangChoices(){
+    if (listLength) {
+      opera.extension.postMessage(("languagesAvailable: " + listLength));
+      opera.extension.postMessage(("longest: " + longString));
+    } else
+      opera.extension.postMessage("none");
+  }
+  //------------------------------------
+  
+  
+  //------------------------------------
+  //
+  function setupChannel( shortTermPort ){
+    channel = new MessageChannel();
+    channel.port1.onmessage = function( event ){
+      window.location.href = event.data;
+    }
+    shortTermPort.postMessage( JSON.stringify(tempObj), [channel.port2] );
+  }
+  //------------------------------------
+  
+    
+  //------------------------------------
+  //Handle onmessage requests
+  opera.extension.onmessage = function( event ){
+    switch( event.data ){
+      case "reportLangChoices":
+        reportLangChoices();
+      break;
+      case "setupChannel":
+        setupChannel( event.ports[0] );
+      break;
+    }
+  }
+  //------------------------------------
+}, false);
+<!DOCTYPE html>
+<head>
+<meta name="Content-Type" content="text/html; charset = UTF-8">
+<title>Swaplang background page</title>
+<style>
+  :link {text-decoration:none}
+  a:hover {background: #ddd;}
+  * {margin: 0px ; padding: 0px}
+  p { line-height: 1.4em}
+  li { font-family: sans; margin-left: 1.5em; }
+</style>
+</head>
+<body>
+<ul><li id="item">dummy</li></ul>
+
+<script>
+
+  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",
+    icon: "icno.png",
+    popup: {
+      href: "popup.html",
+      height: 10, width: 10
+    }
+  }
+  theButton = opera.contexts.toolbar.createItem(UIItemProperties);
+  opera.contexts.toolbar.addItem(theButton);
+  
+  function enableButton(){
+    theButton.disabled = false;
+    theButton.icon = "icon.png";
+  }
+  function disableButton(){
+    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 = function( event ){
+    var currentTab = opera.extension.tabs.getFocused();
+    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") {
+      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
+  opera.extension.onconnect = function( event ){
+    var currentTab = opera.extension.tabs.getFocused();
+    if (currentTab && event.origin.indexOf("popup.html") > -1 && event.origin.indexOf('widget://') > -1) {
+      currentTab.postMessage( "setupChannel", [event.source] );
+    }
+  }
+  //------------------------------------
+</script>

locales/de/popup (Autosaved)

+<!DOCTYPE html>
+<meta 
+<head>
+<title>Swaplang Popup</title>
+<style>
+  :link {text-decoration:none}
+  a:hover {background: #ddd;}
+  * {margin: 0px ; padding: 0px}
+  p { line-height: 1.4em}
+  li { font-family: sans-serif; margin-left: 1.5em; }
+</style>
+</head>
+<body>
+<p id="para">Es gibt auch:</p>
+<ul id="list"></ul>
+</body>
+<script>
+  var portToUserJS;
+  //------------------------------------
+  //Direct messages are always from the userJS.
+  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 theList = document.getElementById('list');
+      for (i in languageChoices) {
+      //JSON data has a length and item attribute, which has to be ignored.
+        if(i == "length" || i == "item") continue;
+      
+      //Add a title, if none exists.
+        if (!(languageChoices[i].title)) {
+          languageChoices[i].title = "Wechseln zu: " + languageChoices[i].hreflang ;
+        }
+      
+      //Create links
+        var theItem = document.createElement("li");
+        var theLink = document.createElement('a');
+        theLink.textContent = languageChoices[i].title;
+        theLink.href = languageChoices[i].href;
+        theLink.onclick = function(event) {
+          opera.postError(this.href);
+          portToUserJS.postMessage(this.href);
+          event.preventDefault();
+          window.close();
+        }
+        theItem.appendChild(theLink);
+        theList.appendChild(theItem);
+      }
+  }
+  //------------------------------------
+</script>

locales/de/popup.html

+<!DOCTYPE html>
+<head>
+<meta name="Content-Type" content="text/html; charset = UTF-8">
+<title>Swaplang Popup</title>
+<style>
+  :link {text-decoration:none}
+  a:hover {background: #ddd;}
+  * {margin: 0px ; padding: 0px}
+  p { line-height: 1.4em}
+  li { font-family: sans-serif; margin-left: 1.5em; }
+</style>
+</head>
+<body>
+<p id="para">Es gibt auch:</p>
+<ul id="list"></ul>
+</body>
+<script>
+  var portToUserJS;
+  //------------------------------------
+  //Direct messages are always from the userJS.
+  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 theList = document.getElementById('list');
+      for (i in languageChoices) {
+      //JSON data has a length and item attribute, which has to be ignored.
+        if(i == "length" || i == "item") continue;
+      
+      //Add a title, if none exists.
+        if (!(languageChoices[i].title)) {
+          languageChoices[i].title = "Wechseln zu: " + languageChoices[i].hreflang ;
+        }
+      
+      //Create links
+        var theItem = document.createElement("li");
+        var theLink = document.createElement('a');
+        theLink.textContent = languageChoices[i].title;
+        theLink.href = languageChoices[i].href;
+        theLink.onclick = function(event) {
+          opera.postError(this.href);
+          portToUserJS.postMessage(this.href);
+          event.preventDefault();
+          window.close();
+        }
+        theItem.appendChild(theLink);
+        theList.appendChild(theItem);
+      }
+  }
+  //------------------------------------
+</script>

locales/es/.DS_Store

Binary file added.

locales/es/popup.html

+<!DOCTYPE html>
+<head>
+<meta name="Content-Type" content="text/html; charset = UTF-8">
+<title>Swaplang Popup - español</title>
+<style>
+  :link {text-decoration:none}
+  a:hover {background: #ddd;}
+  * {margin: 0px ; padding: 0px}
+  p { line-height: 1.4em}
+  li { font-family: sans-serif; margin-left: 1.5em; }
+</style>
+</head>
+<body>
+<p id="para">También hay:</p>
+<ul id="list"></ul>
+</body>
+<script>
+  var portToUserJS;
+  //------------------------------------
+  //Direct messages are always from the userJS.
+  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 theList = document.getElementById('list');
+      for (i in languageChoices) {
+      //JSON data has a length and item attribute, which has to be ignored.
+        if(i == "length" || i == "item") continue;
+      
+      //Add a title, if none exists.
+        if (!(languageChoices[i].title)) {
+          languageChoices[i].title = "Idioma: " + languageChoices[i].hreflang ;
+        }
+      
+      //Create links
+        var theItem = document.createElement("li");
+        var theLink = document.createElement('a');
+        theLink.textContent = languageChoices[i].title;
+        theLink.href = languageChoices[i].href;
+        theLink.onclick = function(event) {
+          opera.postError(this.href);
+          portToUserJS.postMessage(this.href);
+          event.preventDefault();
+          window.close();
+        }
+        theItem.appendChild(theLink);
+        theList.appendChild(theItem);
+      }
+  }
+  //------------------------------------
+</script>

locales/fr/popup.html

+<!DOCTYPE html>
+<head>
+<meta name="Content-Type" content="text/html; charset = UTF-8">
+<title>Swaplang Popup</title>
+<style>
+  :link {text-decoration:none}
+  a:hover {background: #ddd;}
+  * {margin: 0px ; padding: 0px}
+  p { line-height: 1.4em}
+  li { font-family: sans-serif; margin-left: 1.5em; }
+</style>
+</head>
+<body>
+<p id="para">Il y a aussi:</p>
+<ul id="list"></ul>
+</body>
+<script>
+  var portToUserJS;
+  //------------------------------------
+  //Direct messages are always from the userJS.
+  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 theList = document.getElementById('list');
+      for (i in languageChoices) {
+      //JSON data has a length and item attribute, which has to be ignored.
+        if(i == "length" || i == "item") continue;
+      
+      //Add a title, if none exists.
+        if (!(languageChoices[i].title)) {
+          languageChoices[i].title = "Changer �: " + languageChoices[i].hreflang ;
+        }
+      
+      //Create links
+        var theItem = document.createElement("li");
+        var theLink = document.createElement('a');
+        theLink.textContent = languageChoices[i].title;
+        theLink.href = languageChoices[i].href;
+        theLink.onclick = function(event) {
+          opera.postError(this.href);
+          portToUserJS.postMessage(this.href);
+          event.preventDefault();
+          window.close();
+        }
+        theItem.appendChild(theLink);
+        theList.appendChild(theItem);
+      }
+  }
+  //------------------------------------
+</script>
+<!DOCTYPE html>
+<head>
+<meta name="Content-Type" content="text/html; charset = UTF-8">
+<title>Swaplang Popup</title>
+<style>
+  :link {text-decoration:none}
+  a:hover {background: #ddd;}
+  * {margin: 0px ; padding: 0px}
+  p { line-height: 1.4em}
+  li { font-family: sans-serif; margin-left: 1.5em; }
+</style>
+</head>
+<body>
+<p id="para">Also available:</p>
+<ul id="list"></ul>
+</body>
+<script>
+  var portToUserJS;
+  //------------------------------------
+  //Direct messages are always from the userJS.
+  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 theList = document.getElementById('list');
+      for (i in languageChoices) {
+      //JSON data has a length and item attribute, which has to be ignored.
+        if(i == "length" || i == "item") continue;
+      
+      //Add a title, if none exists.
+        if (!(languageChoices[i].title)) {
+          languageChoices[i].title = "Change to " + languageChoices[i].hreflang ;
+        }
+      
+      //Create links
+        var theItem = document.createElement("li");
+        var theLink = document.createElement('a');
+        theLink.textContent = languageChoices[i].title;
+        theLink.href = languageChoices[i].href;
+        theLink.onclick = function(event) {
+          opera.postError(this.href);
+          portToUserJS.postMessage(this.href);
+          event.preventDefault();
+          window.close();
+        }
+        theItem.appendChild(theLink);
+        theList.appendChild(theItem);
+      }
+  }
+  //------------------------------------
+</script>