tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

icon-js.js (3785B)


      1 /**
      2 * @param html {String} HTML representing a single element
      3 * @return {Element}
      4 */
      5 function htmlToElement(html) {
      6  let template = document.createElement("template");
      7  html = html.trim(); // Never return a text node of whitespace as the result
      8  template.innerHTML = html;
      9  //firstChild may be a comment so we must use firstElementChild to avoid picking it
     10  return template.content.firstElementChild;
     11 }
     12 
     13 /**
     14 * Converts an Android Vector Drawable to a standard SVG by striping off or renaming some elements
     15 *
     16 * @param s {String} String of an Android Vector Drawable
     17 * @returns {String} The same string but in the standard SVG representation
     18 */
     19 function androidSVGtoNormalSVG(s) {
     20  s = s.replace(/<\?xml version="1\.0" encoding="utf-8"\?>/g, "");
     21  s = s.replace(
     22    /<vector xmlns:android="http:\/\/schemas.android.com\/apk\/res\/android"/g,
     23    '<svg xmlns="http://www.w3.org/2000/svg"'
     24  );
     25  s = s.replace(/<\/vector>/g, "</svg>");
     26  s = s.replace(/android:(height|width)="(\d+)dp"/g, "");
     27  s = s.replace(/android:viewportHeight="(\d+\.?\d+)"/g, 'height="$1"');
     28  s = s.replace(/android:viewportWidth="(\d+\.?\d+)"/g, 'width="$1"');
     29  s = s.replace(/android:fillColor=/g, "fill=");
     30  s = s.replace(/android:pathData=/g, "d=");
     31  //s = s.replace(/android:/g, '');
     32  return s;
     33 }
     34 
     35 function addToTable(name, svg) {
     36  let table = document.querySelector("#preview_table > tbody");
     37  let row = htmlToElement("<tr></tr>");
     38  row.appendChild(htmlToElement("<td>" + name + "</td>"));
     39  let td = htmlToElement("<td></td>");
     40  td.appendChild(svg);
     41  row.appendChild(td);
     42  table.appendChild(row);
     43 }
     44 
     45 function addSingleItemToTable(str) {
     46  let table = document.querySelector("#preview_table > tbody");
     47  table.append(htmlToElement("<tr><td colspan='2'>" + str + "</td></tr>"));
     48 }
     49 
     50 function getFile(iconName, downloadUrl) {
     51  return new Promise((resolve, reject) => {
     52    let request = new XMLHttpRequest();
     53    request.open("GET", downloadUrl, true);
     54    request.onreadystatechange = function () {
     55      if (request.readyState === 4 && request.status === 200) {
     56        let androidXmlText = request.responseText;
     57        androidXmlText = androidSVGtoNormalSVG(androidXmlText);
     58        resolve([iconName, androidXmlText]);
     59      } else if (request.readyState === 4) {
     60        //Request completed with an error
     61        resolve([iconName, "<span>Error during download</span>"]);
     62      }
     63    };
     64    request.send(null);
     65  });
     66 }
     67 
     68 // This function recovers all icons inside the drawable folder via github API
     69 (function getIcons() {
     70  let request = new XMLHttpRequest();
     71  request.open(
     72    "GET",
     73    "https://api.github.com/repos/mozilla-mobile/android-components/contents/components/ui/icons/src/main/res/drawable",
     74    true
     75  );
     76  //Explicit request of the V3 version of the API
     77  request.setRequestHeader("Accept", "application/vnd.github.v3+json");
     78  request.onreadystatechange = function () {
     79    if (request.readyState === XMLHttpRequest.DONE && request.status === 200) {
     80      let response = JSON.parse(request.response);
     81      if (response.message) {
     82        //Something went wrong
     83        addSingleItemToTable("Error: " + response.message);
     84        return;
     85      }
     86      addSingleItemToTable("Loading");
     87      let promises = [];
     88      for (let i = 0; i < response.length; i++) {
     89        let iconName = response[i].name.substr(0, response[i].name.length - 4);
     90        promises.push(getFile(iconName, response[i].download_url));
     91      }
     92      Promise.all(promises).then(values => {
     93        document.querySelector("#preview_table > tbody").innerHTML = "";
     94        for (let i = 0; i < values.length; i++) {
     95          let name = values[i][0],
     96            svg = values[i][1];
     97          addToTable(name, htmlToElement(svg));
     98        }
     99      });
    100    }
    101  };
    102  request.send(null);
    103 })();