Source

swaplang / popup.html

Full commit
<!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; line-height:1em}
  p { line-height: 1.4em}
  li { font-family: sans-serif; margin-left: 16px; line-height:1.1em}
</style>
<script src="strings.js"></script>
</head>
<body>
<p id="para">Also available:</p>
<ul id="list"></ul>
</body>
<script>
  //localise
  var theP = document.getElementById('para');
  para.innerText = stringSet.alsoAvailable;

  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 = stringSet.changeTo + 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>