Konwerter $ do PLN w AliExpress

Konwertuje wartosci

  1. // ==UserScript==
  2. // @name Konwerter $ do PLN w AliExpress
  3. // @description Konwertuje wartosci
  4. // @author Barricade
  5. // @namespace https://greasyfork.org/pl/scripts/6940-konwerter-do-pln-w-aliexpress
  6. // @grant GM_xmlhttpRequest
  7. // @include http://www.aliexpress.com/*
  8. // @version 0.0.1.20141211230146
  9. // ==/UserScript==
  10.  
  11. GM_xmlhttpRequest ( {
  12. method: "GET",
  13. url: 'http://rate-exchange.appspot.com/currency?from=USD&to=PLN',
  14. //Google sends malformed response, not JSON.
  15. //url: 'http://www.google.com/ig/calculator?hl=en&q=1usd=?inr',
  16.  
  17. onload: function (rsp){
  18. var rspJSON = JSON.parse (rsp.responseText);
  19. var convRate = rspJSON.rate;
  20. console.log (rspJSON, convRate);
  21.  
  22. changeDollarsToPln (document.body, convRate);
  23. }
  24. } );
  25.  
  26. function changeDollarsToPln (node, convRate) {
  27. if (node.nodeType === Node.TEXT_NODE) {
  28. if (/\$/.test (node.nodeValue) ) {
  29. processTextNode (node, convRate);
  30. }
  31. }
  32. else if (node.nodeType === Node.ELEMENT_NODE) {
  33. for (var K = 0, numNodes = node.childNodes.length; K < numNodes; ++K) {
  34. changeDollarsToPln (node.childNodes[K], convRate);
  35. }
  36. }
  37. }
  38.  
  39. function processTextNode (node, convRate) {
  40. /*-- Results like:
  41. ["Three values: ", "$1.10", " ", "$2.20", " ", "$3.00.", ""]
  42. */
  43. var moneySplit = node.nodeValue.split (/US ((?:\+|\-)?\$[0-9.,]+)/);
  44. if (moneySplit && moneySplit.length > 2) {
  45. /*-- Money values will be odd array index, loop through
  46. and convert all.
  47. */
  48. for (var J = 1, L = moneySplit.length; J < L; J += 2) {
  49. var dolVal = parseFloat (moneySplit[J].replace (/\$|,|([.,]$)/g, "") );
  50.  
  51. if (typeof dolVal === "number") {
  52. //var plnVal = Math.round (dolVal * convRate) + "zł";
  53. var plnVal = (dolVal * convRate).toFixed (2) + "zł";
  54. }
  55. else {
  56. var plnVal = moneySplit[J] + " *Err*";
  57. }
  58. moneySplit[J] = plnVal;
  59. }
  60. //-- Rebuild and replace the text node with the changed value (s).
  61. var newTxt = moneySplit.join ("");
  62. node.nodeValue = newTxt;
  63. }
  64. }