shujo / amazon-pdp-extractor

// ==UserScript==
// @name         amazon-pdp-extractor
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       josh
// @match        https://www.amazon.com/dp/*
// @match        https://www.amazon.com/*/dp/*
// @grant        GM_notification
// @run-at       document-end
// @require      http://code.jquery.com/jquery-3.3.1.min.js
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
    document.body.onkeyup = function(e){
        if(e.ctrlKey && e.keyCode == 13){ //keyboard shortcut: ctrl key + enter
            let mergeData = "";
            let brand = $('#bylineInfo_feature_div > div > a').attr("href").split("/")[1];
            let category = verifyData($('#wayfinding-breadcrumbs_feature_div').text());
            let childTitle = verifyData($('#imgTagWrapperId > img').attr("alt"));
            let parentTitle = verifyData($('#productTitle').text());
            let mainImage = verifyData($('#imgTagWrapperId > img').attr("src"));
            let bullets = [];
             $('#feature-bullets > ul > li').each(function(){
               let bullet = verifyData($(this).text());
               bullets.push(bullet)
             });
            let description = verifyData($('#productDescription').text());

            let data = brand + "\t" + category + "\t" + parentTitle + "\t" + childTitle + "\t" + mainImage + "\t" + description + "\t" + bullets.join("|");
            const els = document.createElement('textarea');
            els.value = data;
            document.body.appendChild(els);
            els.select();
            document.execCommand('copy');
            document.body.removeChild(els)
            GM_notification( { title: "Info", text: "Copied Successfully!", timeout: 2000, image: "https://img.icons8.com/color/50/000000/paste.png" } );
        }
    }
})();

let verifyData = (data) =>{
	if(data!=undefined){
        return data.replace(/\n|\t|\r|…/g,'').replace(/\s+/g,' ').trim();
	}else{
        return "n/a";
	}
}