[HFR] SFBA descriptor

Copie la description de l'article lorsque une url appropriée est présente dans le presse papier

目前为 2017-10-01 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name [HFR] SFBA descriptor
  3. // @namespace sfba.hfr
  4. // @version 0.23
  5. // @description Copie la description de l'article lorsque une url appropriée est présente dans le presse papier
  6. // @include http://forum.hardware.fr/message.php*
  7. // @include http://forum.hardware.fr/forum2.php*
  8. // @include http://forum.hardware.fr/hfr/*
  9. // @connect *
  10. // @grant GM_info
  11. // @grant GM_deleteValue
  12. // @grant GM_getValue
  13. // @grant GM_listValues
  14. // @grant GM_setValue
  15. // @grant GM_getResourceText
  16. // @grant GM_getResourceURL
  17. // @grant GM_addStyle
  18. // @grant GM_log
  19. // @grant GM_openInTab
  20. // @grant GM_registerMenuCommand
  21. // @grant GM_setClipboard
  22. // @grant GM_xmlhttpRequest
  23. // @require http://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js
  24. // ==/UserScript==
  25.  
  26. function init_throbber(){
  27. GM_addStyle("#hiu_throbber_sfba{position:fixed;left:0;top:0;background-color:#242424;z-index:1001;" +
  28. "display:none;opacity:0;transition:opacity 0.7s ease 0s;}");
  29. GM_addStyle("#hiu_throbber_img_sfba{position:fixed;left:calc(50% - 64px);top:calc(50% - 64px);width:128px;height:128px;z-index:1002;" +
  30. "display:none;opacity:0;transition:opacity 0.7s ease 0s;border:0;padding:0;}");
  31. GM_addStyle(".hfr_apercu_nope{display:none !important;}");
  32. var throbber_image_url = "http://reho.st/self/30271dc1b7cac925aeabb89fa70e1e17cf5e1840.png";
  33. var hiu_throbber_img_sfba = new Image();
  34. hiu_throbber_img_sfba.src = throbber_image_url;
  35. hiu_throbber_img_sfba.setAttribute("id", "hiu_throbber_img_sfba");
  36. var hiu_throbber_sfba = document.createElement("div");
  37. hiu_throbber_sfba.setAttribute("id", "hiu_throbber_sfba");
  38. hiu_throbber_sfba.appendChild(hiu_throbber_img_sfba);
  39. hiu_throbber_sfba.addEventListener("transitionend", throbber_transitionend, false);
  40. document.body.appendChild(hiu_throbber_sfba);
  41. }
  42.  
  43. function display_throbber(){
  44. if(document.querySelector("div#apercu_reponse")){
  45. document.querySelector("div#apercu_reponse").classList.add("hfr_apercu_nope");
  46. }
  47. hiu_throbber_img_sfba.style.display = "block";
  48. hiu_throbber_sfba.style.display = "block";
  49. hiu_throbber_sfba.style.width = document.documentElement.scrollWidth + "px";
  50. hiu_throbber_sfba.style.height = document.documentElement.scrollHeight + "px";
  51. hiu_throbber_img_sfba.style.opacity = "1";
  52. hiu_throbber_sfba.style.opacity = "0.8";
  53. }
  54.  
  55. function throbber_transitionend() {
  56. if(hiu_throbber_sfba.style.opacity === "0") {
  57. hiu_throbber_img_sfba.style.display = "none";
  58. hiu_throbber_sfba.style.display = "none";
  59. if(document.querySelector("div#apercu_reponse")){
  60. document.querySelector("div#apercu_reponse").classList.remove("hfr_apercu_nope");
  61. }
  62. }
  63. }
  64.  
  65. function hide_throbber(){
  66. hiu_throbber_img_sfba.style.opacity = "0";
  67. hiu_throbber_sfba.style.opacity = "0";
  68. }
  69.  
  70. function insert_text_at_cursor (textarea, text) {
  71. var start = textarea.selectionStart;
  72. var end = textarea.selectionEnd;
  73. textarea.value = textarea.value.substr (0, start) + text + textarea.value.substr (end);
  74. textarea.setSelectionRange (start + text.length, start + text.length);
  75. }
  76.  
  77. function isUrlValid(url) {
  78. var valid = /^(https?|s?ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(url);
  79.  
  80. var parsable = false;
  81. if (valid)
  82. {
  83. if (url.indexOf("amazon") !== -1)
  84. {
  85. parsable = true;
  86. }
  87. else if (url.indexOf("aliexpress") !== -1)
  88. {
  89. parsable = true;
  90. }
  91. else if (url.indexOf("dealabs") !== -1)
  92. {
  93. parsable = true;
  94. }
  95. else if (url.indexOf("gearbest") !== -1)
  96. {
  97. parsable = true;
  98. }
  99. else if (url.indexOf("banggood") !== -1)
  100. {
  101. parsable = true;
  102. }
  103. }
  104. return (valid & parsable);
  105. }
  106.  
  107. function extractHostname(url) {
  108. var hostname;
  109. //find & remove protocol (http, ftp, etc.) and get hostname
  110.  
  111. if (url.indexOf("://") > -1) {
  112. hostname = url.split('/')[2];
  113. }
  114. else {
  115. hostname = url.split('/')[0];
  116. }
  117.  
  118. //find & remove port number
  119. hostname = hostname.split(':')[0];
  120. //find & remove "?"
  121. hostname = hostname.split('?')[0];
  122.  
  123. return hostname;
  124. }
  125.  
  126. function parseAmazon(doc)
  127. {
  128. var img = "";
  129. var title = doc.find("#productTitle").text().trim();
  130. var imgJson = doc.find("#imgTagWrapperId").children().attr('data-a-dynamic-image');
  131. if (imgJson)
  132. img = Object.keys(JSON.parse(imgJson))[0];
  133. var price = doc.find(".a-size-medium.a-color-price").eq(0).text().trim();
  134. if (title && img && price)
  135. return title + " - " + price + "[/url]\n[img]http://reho.st/" + img + "[/img]";
  136. else
  137. return "";
  138. }
  139.  
  140. function parseAli(doc)
  141. {
  142. var title = doc.find(".product-name").text().trim();
  143. var img = doc.find(".ui-image-viewer-thumb-frame").children().attr('src');
  144. var price = doc.find("#j-sku-discount-price").text().trim() + doc.find(".p-symbol").text()[0].trim();
  145. if (title && img && price)
  146. return title + " - " + price + "[/url]\n[img]http://reho.st/" + img + "[/img]";
  147. else
  148. return "";
  149. }
  150.  
  151. function parseDealabs(doc)
  152. {
  153. var title = doc.find("h1.title").text().trim();
  154. var img = doc.find("#over").children().attr('src');
  155. var price = doc.find(".deal_price").text().trim();
  156. var oldPrice = "";
  157. try
  158. {
  159. oldPrice = doc.find(".original_price").text().trim();
  160. }
  161. catch(err){}
  162. if (title && img && price)
  163. {
  164. if (oldPrice)
  165. {
  166. oldPrice = oldPrice.replace(" ", "[/strike] ");
  167. return title + "[/url]\n[img]http://reho.st/" + img + "[/img]\n[b][#FF0000]" + price + "[/#FF0000][/b] [strike]" + oldPrice;
  168. }
  169. else
  170. return title + "[/url]\n[img]http://reho.st/" + img + "[/img]\n[b][#FF0000]" + price + "[/#FF0000][/b]";
  171. }
  172. else
  173. return "";
  174. }
  175.  
  176. function parseGearbest(doc)
  177. {
  178. var title = doc.find("h1").text().trim();
  179. var img = doc.find("#new_addcart").attr('data-proimg');
  180. var price = doc.find("#unit_price").text().trim();
  181. if (title && img && price)
  182. return title + " - " + price + "[/url]\n[img]" + img + "[/img]";
  183. else
  184. return "";
  185. }
  186.  
  187. function parseBanggood(doc)
  188. {
  189. var title = doc.find("h1").text().trim();
  190. var img = doc.find("img.left_largerView_image_20161213").attr('src');
  191. var price = doc.find(".now").text().trim() + doc.find(".top_price_datalist_20161213").children().first().text().trim();
  192. if (title && img && price)
  193. return title + " - " + price + "[/url]\n[img]" + img + "[/img]";
  194. else
  195. return "";
  196. }
  197.  
  198.  
  199. init_throbber();
  200.  
  201. function pasting (event) {
  202. var url = "";
  203. var bbcode = "";
  204. if (event.clipboardData.files.length < 1)
  205. {
  206. var str = event.clipboardData.getData("text/plain");
  207. if ( (str.length === 0) || (!isUrlValid(str)) )
  208. return;
  209. else
  210. url = str;
  211. }
  212. else
  213. {
  214. return;
  215. }
  216. // chargement
  217. display_throbber();
  218. GM_xmlhttpRequest({
  219. method : "GET",
  220. url : url,
  221. context : {
  222. textarea : this
  223. },
  224. onabort : hide_throbber,
  225. onerror : hide_throbber,
  226. ontimeout : hide_throbber,
  227. onload : function (response) {
  228. // fin du chargement
  229. hide_throbber();
  230. if (extractHostname(url).indexOf("amazon") !== -1)
  231. {
  232. bbcode = parseAmazon($(response.responseText));
  233. }
  234. else if (extractHostname(url).indexOf("aliexpress") !== -1)
  235. {
  236. bbcode = parseAli($(response.responseText));
  237. }
  238. else if (extractHostname(url).indexOf("dealabs") !== -1)
  239. {
  240. bbcode = parseDealabs($(response.responseText));
  241. }
  242. else if (extractHostname(url).indexOf("gearbest") !== -1)
  243. {
  244. bbcode = parseGearbest($(response.responseText));
  245. }
  246. else if (extractHostname(url).indexOf("banggood") !== -1)
  247. {
  248. bbcode = parseBanggood($(response.responseText));
  249. }
  250. if (bbcode)
  251. insert_text_at_cursor(this.context.textarea, "[url=" + url + "]" + bbcode);
  252. else
  253. insert_text_at_cursor(this.context.textarea, "[url]" + url + "[/url]");
  254. }
  255. });
  256. return event.preventDefault();
  257. }
  258.  
  259. var content_form = document.querySelector("#content_form");
  260. if (content_form !== null)
  261. content_form.addEventListener('paste', pasting);
  262.  
  263.  
  264.  
  265.  
  266. var observer=new MutationObserver(function(mutations, observer){
  267. var textareas=document.querySelectorAll("textarea[id^=\"rep_editin_\"]" );
  268. if(textareas.length){
  269. for(var textarea of textareas) {
  270. textarea.removeEventListener('paste', pasting, false);
  271. textarea.addEventListener('paste', pasting, false);
  272. }
  273. }
  274. });
  275. observer.observe(document, {attributes: false, childList: true, characterData: false, subtree: true});

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址