Source

swaplang / index.html

<!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>