e14matt / CPC Staff Prices

// ==UserScript==
// @name            CPC Staff Prices
// @namespace       https://cpc.co.uk
// @include         http://cpc.farnell.com/*
// @include         https://cpc.farnell.com/*
// @description     Script to show staff prices inline -- won't work unless you're an employee on the company network. Written by Matt Collinge.
// @require         http://code.jquery.com/jquery-latest.js
// @copyright       Matt Collinge
// @author          e14matt
// @license MIT
// @grant           GM_xmlhttpRequest
// @connect         ontrack.premierfarnell.net
// @updateURL       https://openuserjs.org/meta/e14matt/CPC_Staff_Prices.meta.js
// @version         2.3
// ==/UserScript==

// GUID = 64a6bc68-0d6a-4df1-a22f-18ab15ff265f

//debugger;

if ($('#sProdList').length>0) {
	$('#sProdList .sku a').each(function() {
		var $productCode = $(this).attr('title');
		console.log($productCode);
		$(this).after($('<br><a href="http://ontrack.premierfarnell.net/cpc/index.php?find='+$productCode+'&mode=search" title="Staff price page" target="_blank" style="color:green;">&raquo; View on Intranet</a>'));
	});
}

if ($('.mainPdpWrapper').length>0) {
    // NEW WAY - LOOK AT URL
    var productCode = window.location.pathname.split('/')[window.location.pathname.split('/').length-1]
    console.log(productCode);
    if (productCode!=='') {
        $('<a href="http://ontrack.premierfarnell.net/cpc/index.php?find='+productCode+'&mode=search" title="Staff price page" style="padding-left:13px;color:green;" target="_blank">&raquo; View on Intranet</a><br><br>').insertAfter('.productPrice');
        $('<p id="staffPrice" style="padding-top:5px">Fetching data from staff price server...</p>').insertAfter('.productPrice');
        retrieveStaffPrice(productCode);
    }

    /* OLD WAY LOOKING AT DOM
    $('.productDescription dd[itemprop="http://schema.org/sku"]').each(function(index) {
		var productCode = $(this).html();
		productCode = productCode.replace(/ /g, '');
		productCode = productCode.replace(/\t/g, '');
		productCode = productCode.replace(/\n/g, '');
		console.log(productCode);
		if (productCode!='') {
			$('<a href="http://ontrack.premierfarnell.net/cpc/index.php?find='+productCode+'&mode=search" title="Staff price page" style="padding-left:13px;color:green;" target="_blank">&raquo; View on Intranet</a><br><br>').insertAfter('.productPrice');
			$('<p id="staffPrice" style="padding-top:5px">Fetching staff price from Intranet...</p>').insertAfter('.productPrice');
			retrieveStaffPrice(productCode);
		}
	});*/
}

function retrieveStaffPrice(skus) {

	GM_xmlhttpRequest({
		method: 'GET',
		url: 'http://ontrack.premierfarnell.net/cpc/indexAPI.php?find='+skus+'&mode=search',
		headers: {
			'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
			'Accept': 'application/atom+xml,application/xml,text/xml',
		},
		onload: function(responseDetails) {
			var responseJSON = $.parseJSON(responseDetails.responseText);
			if (responseJSON.count=='0') {
				$('#staffPrice').html('Unable to find staff price for this item');
			} else {
				$('#staffPrice').html('<font color="green" size=3><b>Staff Price: </b>£' + responseJSON.items[0].priceIncVat + ' inc VAT</font>');
			}
		}
	});

}