[HFR] SFBA descriptor

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

目前為 2017-10-01 提交的版本,檢視 最新版本

// ==UserScript==
// @name         [HFR] SFBA descriptor
// @namespace    sfba.hfr
// @version      0.23
// @description  Copie la description de l'article lorsque une url appropriée est présente dans le presse papier
// @include       http://forum.hardware.fr/message.php*
// @include       http://forum.hardware.fr/forum2.php*
// @include       http://forum.hardware.fr/hfr/*
// @connect       *
// @grant         GM_info
// @grant         GM_deleteValue
// @grant         GM_getValue
// @grant         GM_listValues
// @grant         GM_setValue
// @grant         GM_getResourceText
// @grant         GM_getResourceURL
// @grant         GM_addStyle
// @grant         GM_log
// @grant         GM_openInTab
// @grant         GM_registerMenuCommand
// @grant         GM_setClipboard
// @grant         GM_xmlhttpRequest
// @require       http://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js
// ==/UserScript==

function init_throbber(){
  GM_addStyle("#hiu_throbber_sfba{position:fixed;left:0;top:0;background-color:#242424;z-index:1001;" +
              "display:none;opacity:0;transition:opacity 0.7s ease 0s;}");
  GM_addStyle("#hiu_throbber_img_sfba{position:fixed;left:calc(50% - 64px);top:calc(50% - 64px);width:128px;height:128px;z-index:1002;" +
              "display:none;opacity:0;transition:opacity 0.7s ease 0s;border:0;padding:0;}");
  GM_addStyle(".hfr_apercu_nope{display:none !important;}");
  var throbber_image_url = "http://reho.st/self/30271dc1b7cac925aeabb89fa70e1e17cf5e1840.png";
  var hiu_throbber_img_sfba = new Image();
  hiu_throbber_img_sfba.src = throbber_image_url;
  hiu_throbber_img_sfba.setAttribute("id", "hiu_throbber_img_sfba");
  var hiu_throbber_sfba = document.createElement("div");
  hiu_throbber_sfba.setAttribute("id", "hiu_throbber_sfba");
  hiu_throbber_sfba.appendChild(hiu_throbber_img_sfba);
  hiu_throbber_sfba.addEventListener("transitionend", throbber_transitionend, false);
  document.body.appendChild(hiu_throbber_sfba);
}

function display_throbber(){
  if(document.querySelector("div#apercu_reponse")){
    document.querySelector("div#apercu_reponse").classList.add("hfr_apercu_nope");
  }
  hiu_throbber_img_sfba.style.display = "block";
  hiu_throbber_sfba.style.display = "block";
  hiu_throbber_sfba.style.width = document.documentElement.scrollWidth + "px";
  hiu_throbber_sfba.style.height = document.documentElement.scrollHeight + "px";
  hiu_throbber_img_sfba.style.opacity = "1";
  hiu_throbber_sfba.style.opacity = "0.8";
}

function throbber_transitionend() {
  if(hiu_throbber_sfba.style.opacity === "0") {
    hiu_throbber_img_sfba.style.display = "none";
    hiu_throbber_sfba.style.display = "none";
    if(document.querySelector("div#apercu_reponse")){
      document.querySelector("div#apercu_reponse").classList.remove("hfr_apercu_nope");
    }
  }
}

function hide_throbber(){
  hiu_throbber_img_sfba.style.opacity = "0";
  hiu_throbber_sfba.style.opacity = "0";
}

function insert_text_at_cursor (textarea, text) {
	var start = textarea.selectionStart;
	var end = textarea.selectionEnd;
	textarea.value = textarea.value.substr (0, start) + text + textarea.value.substr (end);
	textarea.setSelectionRange (start + text.length, start + text.length);
}

function isUrlValid(url) {
    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);

    var parsable = false;
    
    if (valid)
    {
        if (url.indexOf("amazon") !== -1)
        {
            parsable = true;
        }
        else if (url.indexOf("aliexpress") !== -1)
        {
            parsable = true;
        }
        else if (url.indexOf("dealabs") !== -1)
        {
            parsable = true;
        }
        else if (url.indexOf("gearbest") !== -1)
        {
            parsable = true;
        }
        else if (url.indexOf("banggood") !== -1)
        {
            parsable = true;
        }
    }
    
    return (valid & parsable);
}

function extractHostname(url) {
    var hostname;
    //find & remove protocol (http, ftp, etc.) and get hostname

    if (url.indexOf("://") > -1) {
        hostname = url.split('/')[2];
    }
    else {
        hostname = url.split('/')[0];
    }

    //find & remove port number
    hostname = hostname.split(':')[0];
    //find & remove "?"
    hostname = hostname.split('?')[0];

    return hostname;
}

function parseAmazon(doc)
{
    var img = "";
    var title = doc.find("#productTitle").text().trim();    
    var imgJson = doc.find("#imgTagWrapperId").children().attr('data-a-dynamic-image');
    if (imgJson)
        img = Object.keys(JSON.parse(imgJson))[0];
    var price = doc.find(".a-size-medium.a-color-price").eq(0).text().trim();
    
    if (title && img && price)
        return title + " - " + price + "[/url]\n[img]http://reho.st/" + img + "[/img]";
    else
        return "";
}

function parseAli(doc)
{
    var title = doc.find(".product-name").text().trim();
    var img = doc.find(".ui-image-viewer-thumb-frame").children().attr('src');
    var price = doc.find("#j-sku-discount-price").text().trim() + doc.find(".p-symbol").text()[0].trim();
       
    if (title && img && price)
        return title + " - " + price + "[/url]\n[img]http://reho.st/" + img + "[/img]";
    else
        return "";
}

function parseDealabs(doc)
{
    var title = doc.find("h1.title").text().trim();
    var img = doc.find("#over").children().attr('src');
    var price = doc.find(".deal_price").text().trim();
    var oldPrice = "";
    try 
    {
        oldPrice = doc.find(".original_price").text().trim();
    }
    catch(err){}
    
    if (title && img && price)
    {
        if (oldPrice)
        {
            oldPrice = oldPrice.replace(" ", "[/strike] ");
            return title + "[/url]\n[img]http://reho.st/" + img + "[/img]\n[b][#FF0000]" + price + "[/#FF0000][/b] [strike]" + oldPrice;
        }
        else
            return title + "[/url]\n[img]http://reho.st/" + img + "[/img]\n[b][#FF0000]" + price + "[/#FF0000][/b]";
    }
    else
        return "";
}

function parseGearbest(doc)
{
    var title = doc.find("h1").text().trim();
    var img = doc.find("#new_addcart").attr('data-proimg');
    var price = doc.find("#unit_price").text().trim();
    
    if (title && img && price)
        return title + " - " + price + "[/url]\n[img]" + img + "[/img]";
    else
        return ""; 
}

function parseBanggood(doc)
{
    var title = doc.find("h1").text().trim();
    var img = doc.find("img.left_largerView_image_20161213").attr('src');
    var price = doc.find(".now").text().trim() + doc.find(".top_price_datalist_20161213").children().first().text().trim();
    
    if (title && img && price)
        return title + " - " + price + "[/url]\n[img]" + img + "[/img]";
    else
        return ""; 
}


init_throbber();

function pasting (event) {
    var url = "";
    var bbcode = "";
	if (event.clipboardData.files.length < 1)
    {
		var str = event.clipboardData.getData("text/plain");
		if ( (str.length === 0) || (!isUrlValid(str)) )
			return;
        else
            url = str;
	}
    else
    {
        return;
    }
	
	// chargement
	display_throbber();
	
	GM_xmlhttpRequest({
		method : "GET",
		url : url,
        context : {
			textarea : this
		},
		onabort : hide_throbber,
		onerror : hide_throbber,
		ontimeout : hide_throbber,
		onload : function (response) {
			// fin du chargement
			hide_throbber();
            
            if (extractHostname(url).indexOf("amazon") !== -1)
            {
                bbcode = parseAmazon($(response.responseText));
            }
            else if (extractHostname(url).indexOf("aliexpress") !== -1)
            {
                bbcode = parseAli($(response.responseText));
            }
            else if (extractHostname(url).indexOf("dealabs") !== -1)
            {
                bbcode = parseDealabs($(response.responseText));
            }
            else if (extractHostname(url).indexOf("gearbest") !== -1)
            {
                bbcode = parseGearbest($(response.responseText));
            }
            else if (extractHostname(url).indexOf("banggood") !== -1)
            {
                bbcode = parseBanggood($(response.responseText));
            }
            
            if (bbcode)
                insert_text_at_cursor(this.context.textarea, "[url=" + url + "]" + bbcode);
            else
                insert_text_at_cursor(this.context.textarea, "[url]" + url + "[/url]");
		}
	});
	return event.preventDefault();
	
}

var content_form = document.querySelector("#content_form");
if (content_form !== null)
	content_form.addEventListener('paste', pasting);




var observer=new MutationObserver(function(mutations, observer){ 
  var textareas=document.querySelectorAll("textarea[id^=\"rep_editin_\"]" ); 
  if(textareas.length){ 
    for(var textarea of textareas) { 
      textarea.removeEventListener('paste', pasting, false); 
      textarea.addEventListener('paste', pasting, false); 
    } 
  } 
}); 
observer.observe(document, {attributes: false, childList: true, characterData: false, subtree: true}); 

QingJ © 2025

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