home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Resources / Media / MediaCoder 0.6.0.3690 / MediaCoder-0.6.0.3690.exe / extensions / _include / extension.js < prev    next >
Encoding:
JavaScript  |  2007-04-19  |  5.2 KB  |  261 lines

  1. /******************************************************************
  2. * MediaCoder extension base scripts
  3. * Distributed under GPL license
  4. * Copyright (c) 2007 Stanley Huang <stanleyhuangyc@gmail.com>
  5. * All rights reserved.
  6. ******************************************************************/
  7.  
  8. var xmlhttp = CreateXMLHttp();
  9. var debug;
  10. if (GetToken(document.location.href, "debug")) {
  11.     debug = true;
  12. } else {
  13.     debug = false;
  14. }
  15.  
  16. function CreateXMLHttp()
  17. {
  18.     var newxmlhttp = window.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject("Microsoft.XMLHTTP");
  19.     if (!newxmlhttp) alert("XMLHttpRequest object cannot be created.");
  20.     return newxmlhttp;
  21. }
  22.  
  23. function NewXML(rootkey)
  24. {
  25.     var xmlDoc;
  26.     if (document.implementation && document.implementation.createDocument)
  27.     {
  28.         xmlDoc = document.implementation.createDocument("", rootkey, null);
  29.     }
  30.     else if (window.ActiveXObject)
  31.     {
  32.         xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  33.      }
  34.     else
  35.     {
  36.         return;
  37.     }
  38.     return xmlDoc;
  39. }
  40.  
  41. function LoadXMLFile(xmlfile)
  42. {
  43.     var xmlDoc;
  44.     if (document.implementation && document.implementation.createDocument)
  45.     {
  46.         xmlDoc = document.implementation.createDocument("", "", null);
  47.         //xmlDoc.onload = callback;
  48.     }
  49.     else if (window.ActiveXObject)
  50.     {
  51.         xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  52.         xmlDoc.onreadystatechange = function () {
  53.             //if (xmlDoc.readyState == 4) callback()
  54.         };
  55.      }
  56.     else
  57.     {
  58.         return;
  59.     }
  60.     xmlDoc.async = false;
  61.     try { 
  62.         if (!xmlDoc.load(xmlfile)) return null;
  63.     }
  64.     catch (ex)
  65.     {
  66.         return null;
  67.     }
  68.     return xmlDoc;
  69. }
  70.  
  71. function AddPrefNode(doc, path, value)
  72. {
  73.     var i;
  74.     var tokens = path.split('.');
  75.     var node = doc.firstChild;
  76.     var newnode;
  77.     for (i = 0; i < tokens.length; i++) {
  78.         newnode = doc.createElement("node");
  79.         newnode.setAttribute("key", tokens[i]);
  80.         node = node.appendChild(newnode);
  81.     }
  82.     node.setAttribute("value", value);
  83. }
  84.  
  85. function AddPrefElements(doc, arrayElements)
  86. {
  87.     var i;
  88.     for (i = 0; i < arrayValues.length; i+=2) {
  89.         AddPrefNode(doc, arrayValues[i], arrayValues[i+1]);
  90.     }
  91. }
  92.  
  93. function AddPrefValues(doc, arrayValues)
  94. {
  95.     var i;
  96.     for (i = 0; i < arrayValues.length; i+=2) {
  97.         AddPrefNode(doc, arrayValues[i], arrayValues[i+1]);
  98.     }
  99. }
  100.  
  101. function SetPrefValue(param, val)
  102. {
  103.     xmlhttp.open("GET", "/prefs/set?" + param + "=" + val, false);
  104.     xmlhttp.send(null);
  105.     if (xmlhttp.responseText == "0") {
  106.         alert("Preference key '"+param+"' can't be set to '"+val+"'.");
  107.         return false;
  108.     }
  109.     return true;
  110. }
  111.  
  112. function GetPrefValue(param)
  113. {
  114.     xmlhttp.open("GET", "/prefs/get?path=" + param, false);
  115.     xmlhttp.send(null);
  116.     return xmlhttp.responseText;
  117. }
  118.  
  119. function AlertConnError()
  120. {
  121.     if (debug) alert("Error connecting to MediaCoder.");
  122. }
  123.  
  124. function PostPrefXML(data)
  125. {
  126.     try { 
  127.         xmlhttp.open("POST", "/prefs/import", false);
  128.         xmlhttp.send(data);
  129.         if (xmlhttp.responseText != "OK")
  130.             alert("Error posting preference data to MediaCoder.");
  131.     }
  132.     catch (ex) {
  133.         AlertConnError();
  134.     }
  135. }
  136.  
  137. function SendCommand(cmd)
  138. {
  139.     try { 
  140.         xmlhttp.open("GET", "/ui?cmd=" + cmd, false);
  141.         xmlhttp.send(null);
  142.     }
  143.     catch (ex) {
  144.         AlertConnError();
  145.     }
  146. }
  147.  
  148. function PlayVideo(hwnd)
  149. {
  150.     try { 
  151.         xmlhttp.open("GET", "/ui?cmd=play " + hwnd, false);
  152.         xmlhttp.send(null);
  153.     }
  154.     catch (ex) {
  155.         AlertConnError();
  156.     }
  157. }
  158.  
  159. function StopPlay()
  160. {
  161.     try { 
  162.         xmlhttp.open("GET", "/ui?cmd=stop_play " + hwnd, false);
  163.         xmlhttp.send(null);
  164.     }
  165.     catch (ex) {
  166.         AlertConnError();
  167.     }
  168. }
  169.  
  170. function PlayConvertedVideo(hwnd)
  171. {
  172.     try { 
  173.         xmlhttp.open("GET", "/ui?cmd=preview " + hwnd, false);
  174.         xmlhttp.send(null);
  175.     }
  176.     catch (ex) {
  177.         AlertConnError();
  178.     }
  179. }
  180.  
  181. function QueryString(url, async)
  182. {
  183.     try { 
  184.         xmlhttp.open("GET", url, async);
  185.         xmlhttp.send(null);
  186.         return xmlhttp.responseText;
  187.     }
  188.     catch (ex) {
  189.         AlertConnError();
  190.         return null;
  191.     }
  192. }
  193.  
  194. function QueryInfo(info)
  195. {
  196.     return QueryString("/mc/get?query=" + info, false);
  197. }
  198.  
  199. function QueryPref(path, opts)
  200. {
  201.     var node;
  202.     var requrl = "/prefs/prefs.xml?type&";
  203.     if (opts)
  204.         requrl += opts + "&";
  205.     if (path)
  206.         requrl += "path=" + path;
  207.     xmlhttp.open("GET", requrl, false);
  208.     xmlhttp.send(null);
  209.     node = xmlhttp.responseXML.firstChild;
  210.     if (!node) {
  211.         alert("Failed to retrieve preference data from MediaCoder.");
  212.         return null;
  213.     }
  214.     return getChildNode(node);
  215. }
  216.  
  217. function AddFile(filename)
  218. {
  219.     if (filename) {
  220.         xmlhttp.open("GET", "/queue?action=addfile&file=" + filename, false);
  221.         xmlhttp.send(null);
  222.     }
  223. }
  224.  
  225. function SetWindowSize(width, height)
  226. {
  227.     window.innerWidth = width;
  228.     window.innerHeight = height;
  229. }
  230.  
  231. function OpenURL(urlstring) {
  232.     xmlhttp.open('GET', '/openurl?url=' + urlstring, false);
  233.     xmlhttp.send(null);
  234. }
  235.  
  236. function ShowItem(id)
  237. {
  238.     var e = document.getElementById(id);
  239.     if (e) e.style.display = "block";
  240. }
  241.  
  242. function HideItem(id)
  243. {
  244.     var e = document.getElementById(id);
  245.     if (e) e.style.display = "none";
  246. }
  247.  
  248. function ValueOf(id)
  249. {
  250.     return document.getElementById(id).value;
  251. }
  252.  
  253. function GetToken(str, token)
  254. {
  255.     var idx = str.indexOf(token + '=');
  256.     if (idx < 0) return null;
  257.     var argstr = str.substring(idx + token.length + 1);
  258.     idx = argstr.indexOf('&');
  259.     return idx > 0 ? argstr.substring(0, idx) : argstr;
  260. }
  261.