🏠 返回首頁 

Greasy Fork is available in English.

Amazon конвертер величин

Конвертирует единицы измерения Амазона из американских в нормальные. Он проверяет только 2 строчки в Характеристиках, поэтому этот скрипт не оказывает практически никакого влияния на производительность

  1. // ==UserScript==
  2. // @name Amazon units converter
  3. // @name:ru Amazon конвертер величин
  4. // @namespace https://greasyfork.org/ru/users/303426-титан
  5. // @version 1.1.0
  6. // @description Convert Amazon units to normal units that all normal countries uses. It's check only 2 rows in a Specs table, so it's very perfomance-friendly.
  7. // @description:ru Конвертирует единицы измерения Амазона из американских в нормальные. Он проверяет только 2 строчки в Характеристиках, поэтому этот скрипт не оказывает практически никакого влияния на производительность
  8. // @author Титан
  9. // @include http://www.amazon.com/*/dp/*
  10. // @include https://www.amazon.com/*/dp/*
  11. // @include http://www.amazon.co.uk/*/dp/*
  12. // @include https://www.amazon.co.uk/*/dp/*
  13. // @include http://www.amazon.fr/*/dp/*
  14. // @include https://www.amazon.fr/*/dp/*
  15. // @include http://www.amazon.de/*/dp/*
  16. // @include https://www.amazon.de/*/dp/*
  17. // @include http://www.amazon.es/*/dp/*
  18. // @include https://www.amazon.es/*/dp/*
  19. // @include http://www.amazon.ca/*/dp/*
  20. // @include https://www.amazon.ca/*/dp/*
  21. // @include http://www.amazon.in/*/dp/*
  22. // @include https://www.amazon.in/*/dp/*
  23. // @include http://www.amazon.it/*/dp/*
  24. // @include https://www.amazon.it/*/dp/*
  25. // @include http://www.amazon.co.jp/*/dp/*
  26. // @include https://www.amazon.co.jp/*/dp/*
  27. // @include http://www.amazon.com.mx/*/dp/*
  28. // @include https://www.amazon.com.mx/*/dp/*
  29. // @include http://www.amazon.com.au/*/dp/*
  30. // @include https://www.amazon.com.au/*/dp/*
  31. // @include http://www.amazon.com/*/product/*
  32. // @include https://www.amazon.com/*/product/*
  33. // @include http://www.amazon.co.uk/*/product/*
  34. // @include https://www.amazon.co.uk/*/product/*
  35. // @include http://www.amazon.fr/*/product/*
  36. // @include https://www.amazon.fr/*/product/*
  37. // @include http://www.amazon.de/*/product/*
  38. // @include https://www.amazon.de/*/product/*
  39. // @include http://www.amazon.es/*/product/*
  40. // @include https://www.amazon.es/*/product/*
  41. // @include http://www.amazon.ca/*/product/*
  42. // @include https://www.amazon.ca/*/product/*
  43. // @include http://www.amazon.in/*/product/*
  44. // @include https://www.amazon.in/*/product/*
  45. // @include http://www.amazon.it/*/product/*
  46. // @include https://www.amazon.it/*/product/*
  47. // @include http://www.amazon.co.jp/*/product/*
  48. // @include https://www.amazon.co.jp/*/product/*
  49. // @include http://www.amazon.com.mx/*/product/*
  50. // @include https://www.amazon.com.mx/*/product/*
  51. // @include http://www.amazon.com.au/*/product/*
  52. // @include https://www.amazon.com.au/*/product/*
  53. // @icon https://www.google.com/s2/favicons?domain=amazon.com
  54. // @grant none
  55. // ==/UserScript==
  56. (function() {
  57. 'use strict';
  58. let DebugText = ":::Amazon American units to normal units extension::: ";
  59. let Debug = false;
  60. let UnitNames = ["inches", "pounds", "ounces"];
  61. let NewUnitNames = ["cm", "kg", "gram"]
  62. let ConvertFuncs = [ConvertInchesToSm, ConvertPoundsToKg, ConvertOuncesToGram]
  63. let CheckAllRows = false;
  64. class UnitText {
  65. StartIndex
  66. Unit
  67. NewUnit
  68. UnitConvertFunc
  69. get StartIndex() {
  70. return this._Index;
  71. }
  72. set StartIndex(value) {
  73. this._Index = value;
  74. }
  75. get Unit() {
  76. return this._Unit;
  77. }
  78. set Unit(value) {
  79. this._Unit = value;
  80. }
  81. get NewUnit() {
  82. return this._NewUnit;
  83. }
  84. set NewUnit(value) {
  85. this._NewUnit = value;
  86. }
  87. get UnitConvertFunc() {
  88. return this._UnitConvertFunc;
  89. }
  90. set UnitConvertFunc(value) {
  91. this._UnitConvertFunc = value;
  92. }
  93. constructor(StartIndex, Unit, NewUnit, UnitConvertFunc) {
  94. this._Index = StartIndex;
  95. this._Unit = Unit;
  96. this._NewUnit = NewUnit;
  97. this._UnitConvertFunc = UnitConvertFunc;
  98. }
  99. }
  100. window.onload = () => {
  101. if (Debug) {console.log("Amazon unit converter started"); }
  102. let Tables = document.querySelectorAll("table");
  103. for(let table of Tables) {
  104. if (Debug) {console.log("TABLE:"); console.log(table);}
  105. let tbody = table.firstElementChild;
  106. try {
  107. for (let tr of tbody.children) {
  108. let thNode = tr;
  109. while (thNode.firstElementChild != null) {thNode = thNode.firstElementChild}
  110. let lowerText = thNode.textContent.toLowerCase();
  111. if (Debug) console.log("text: ", lowerText);
  112. if (lowerText.indexOf("dimensions") >= 0 || lowerText.indexOf("weight") >= 0||!CheckAllRows) {
  113. TableSearch(tr);
  114. }
  115. }
  116. }
  117. catch (e) {
  118. if(Debug) console.log(table.id+ ": "+e.message);
  119. }
  120. }
  121. }
  122. function TableSearch(tr) {
  123. if (Debug) {
  124. console.log("TableSearch td:");
  125. console.log(tr);
  126. }
  127. let dimentionsTds = tr.querySelectorAll("td");
  128. for (let td of dimentionsTds) {
  129. while (td.firstElementChild != null) {td = td.firstElementChild}
  130. td.textContent = ReplaceUnits(td.textContent);
  131. }
  132. }
  133. /**
  134. *
  135. * @param {string} Text Text that will be converted
  136. * @returns {string} Is something were converted (False, if nothing to convert found)
  137. * @constructor
  138. */
  139. function ReplaceUnits(Text) {
  140. let lastStart;
  141. do {
  142. let unitTextInfo = new Array(UnitNames.length);
  143. for (let i = 0; i < UnitNames.length; i++) {
  144. unitTextInfo[i] = new UnitText();
  145. unitTextInfo[i].StartIndex = Text.toLowerCase().indexOf(UnitNames[i]);
  146. if (unitTextInfo[i].StartIndex>0) {
  147. unitTextInfo[i].StartIndex+=UnitNames[i].length;
  148. unitTextInfo[i].Unit = UnitNames[i];
  149. unitTextInfo[i].NewUnit = NewUnitNames[i];
  150. unitTextInfo[i].UnitConvertFunc = ConvertFuncs[i];
  151. }
  152. }
  153. unitTextInfo.sort((a, b) => a.StartIndex - b.StartIndex); //5,4,3,2,1
  154. let Texts = new Array(unitTextInfo.length);
  155. lastStart = 0;
  156. for (let i = 0; i < unitTextInfo.length; i++) {
  157. if (unitTextInfo[i].StartIndex != -1) {
  158. Texts[i] = Text.slice(lastStart, unitTextInfo[i].StartIndex + 1);
  159. lastStart = unitTextInfo[i].StartIndex;
  160. Texts[i] = ReplaceUnit(Texts[i], unitTextInfo[i].Unit, unitTextInfo[i].NewUnit, unitTextInfo[i].UnitConvertFunc);
  161. }
  162. }
  163. if (lastStart == 0) break;
  164. Text = "";
  165. for (let t of Texts) {
  166. if (t!=undefined) Text+=t;
  167. }
  168. } while (true) //do until nothing to convert left
  169. return Text;
  170. }
  171. /**
  172. * Converts all numbers in text from old unit to new unit
  173. *
  174. * @param {string} Text Text that will be converted
  175. * @param {string} UnitName Name of current unit
  176. * @param {string} NewUnitName Name of the new unit
  177. * @param {function} UnitConvertFunc Function that takes current unit float and returns new unit float
  178. * @return {string} Text with all old unit floats replaced by new unit floats
  179. */
  180. function ReplaceUnit(Text,UnitName,NewUnitName,UnitConvertFunc) {
  181. let NewText = "";
  182. let lastUnitTextIndex = -UnitName.length;
  183. let unitTextIndex = Text.toLowerCase().indexOf(UnitName);
  184. if (unitTextIndex===-1) return Text;
  185. let unitTextFragment = Text.slice(lastUnitTextIndex+UnitName.length, unitTextIndex)
  186. lastUnitTextIndex = unitTextIndex;
  187. while (unitTextIndex!==-1) { //while there IS a UnitName text in a string untouched
  188. let i = 0;
  189. console.log("original fragment: " + unitTextFragment);
  190. LfragmentLoop:
  191. while (i < unitTextFragment.length)
  192. { //Перебор внутри фрагмента
  193. let firstDigitPos;
  194. let lastDigitPos;
  195. for (i; ;i++) { //Searches for first digit symbol
  196. if (i === unitTextFragment.length) { //if first digit not found, exit circle
  197. if(Debug) console.log(`${DebugText}no Number found before \"${UnitName}\" text: ${NormalizeString( unitTextFragment)}`);
  198. break LfragmentLoop;
  199. }
  200. if (isFloatNumber(unitTextFragment[i])) break; //if first digit found, break
  201. }
  202. lastDigitPos = firstDigitPos = i;
  203. for (i+=1;i < unitTextFragment.length; i++) {//searches for last digit position
  204. if (isFloatNumber(unitTextFragment[i])) lastDigitPos = i;
  205. else break;
  206. }
  207. let UnitNumber = unitTextFragment.slice(firstDigitPos,lastDigitPos+1);
  208. let NewUnitNumber = UnitConvertFunc(UnitNumber).toFixed(2);
  209. unitTextFragment = unitTextFragment.slice(0,firstDigitPos) + NewUnitNumber + unitTextFragment.slice(lastDigitPos+1);
  210. let offset = NewUnitNumber.length - UnitNumber.length
  211. i+= offset;
  212. if(Debug) console.log("fragment: "+ NormalizeString(unitTextFragment)+"\nCursorpos:"+CreateSpaces(i)+"↑");
  213. }
  214. NewText+=unitTextFragment + NewUnitName;
  215. unitTextIndex = Text.toLowerCase().indexOf(UnitName, lastUnitTextIndex+UnitName.length);
  216. if (unitTextIndex===-1) break;
  217. unitTextFragment = Text.slice(lastUnitTextIndex+UnitName.length, unitTextIndex)
  218. lastUnitTextIndex = unitTextIndex;
  219. }
  220. if (lastUnitTextIndex<Text.length)
  221. NewText+=Text.slice(lastUnitTextIndex+UnitName.length)
  222. return NewText;
  223. }
  224. function ConvertInchesToSm(Inches) {
  225. return Inches*2.54;
  226. }
  227. function ConvertPoundsToKg(Pounds) {
  228. return Pounds*0.45359237;
  229. }
  230. function ConvertOuncesToGram(Ounces) {
  231. return Ounces *28.34952313;
  232. }
  233. function isFloatNumber(c) {
  234. return c >= '0' && c <= '9' || c === '.';
  235. }
  236. function CreateSpaces(Count){
  237. let Spaces = "";
  238. for (let i=Count;i--; i>0) Spaces+= " ";
  239. return Spaces;
  240. }
  241. function NormalizeString(inputString){
  242. let normalizedString = "";
  243. for (let i=0; i<inputString.length;i++) {
  244. switch (inputString[i]) {
  245. case '\n': normalizedString+="↵"; break;
  246. case '\t': normalizedString+="⇥"; break;
  247. case '\r': normalizedString+="␍"; break;
  248. case '\b': normalizedString+="␈"; break;
  249. case '\f': normalizedString+="↡"; break;
  250. case '\v': normalizedString+="⭳"; break;
  251. case '\0': normalizedString+="␀"; break;
  252. default: normalizedString+= inputString[i]; break;
  253. }
  254. }
  255. return normalizedString;
  256. }
  257. })();