cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <h1>Scrape list of colours from Wikipedia</h1>
<p>Example:</p>

<div style="padding:3px; display:inline-block; background-color:#0048BA">Absolute Zero</div><div style="padding:3px; display:inline-block; background-color:#B0BF1A">Acid green</div><div style="padding:3px; display:inline-block; background-color:#7CB9E8">Aero</div><div style="padding:3px; display:inline-block; background-color:#C9FFE5">Aero blue</div><div style="padding:3px; display:inline-block; background-color:#B284BE">African violet</div><div style="padding:3px; display:inline-block; background-color:#5D8AA8">Air Force blue (RAF)</div>

<div id="results-anchors"></div>
<div id="results"></div>
            
          
!
            
              html {
  background-color:#EEF;
  margin: 10px;
}

h1 {
  font-size:30px;
}

h1, h2, h3 {
  font-family: Georgia;
}

.html-log {
  font-family: "Fira Mono",Monaco,"Andale Mono","Lucida Console","Bitstream Vera Sans Mono","Courier New",Courier,monospace;
}

.html-log ul {
  list-style-type: none;
  padding-left:20px;
}

body {
  font-family: "Fira Mono",Monaco,"Andale Mono","Lucida Console","Bitstream Vera Sans Mono","Courier New",Courier,monospace;
  color:#224;
  font-size: 13px;
}

#status-log {
  background-color: #DDF;
  padding: 1rem;
  width: 100%;
  max-height: 4rem;
  overflow: auto;
}

#results .results-block {
  background-color: #DDF;
  padding: 1rem;
  margin-bottom: 1rem;
}

#results-anchors {
  padding: 1rem;
}
            
          
!
            
              // htmlLog()
// @item - an object or variable to log to the results panel
var htmlLog = function(item) {
  return '<div class="html-log">' + htmlLogRecursive(item) + '</div>';
};

// htmlLogRecursive()
// @item - an object or variable to log to the results panel
var htmlLogRecursive = function(item) {
  var html = "";
  if (typeof item == "undefined") {
    return "undefined";
  }

  if (typeof item == "function") {
    return "function";
  }

  if (Array.isArray(item)) {
    html += "[<ul>";
    for (var i = 0; i < item.length; i++) {
      html += "<li>" + i + ": " + htmlLogRecursive(item[i]) + "</li>";
    }
    html += "</ul>]";
    return html;
  }

  if (typeof item == "object") {
    if (item === null) {
      return "null";
    }

    html += "{<ul>";
    for (var p in item) {
      html += "<li>" + p + ": " + htmlLogRecursive(item[p]) + "</li>";
    }
    html += "</ul>}";
    return html;
  }

  if (typeof item == "string") {
    return '"' + item + '"';
  }
  return item.toString();
};

// logResults()
// @str - adds a string to the results log
// $title - optional string,title of results set. Anchor is created for each of these
var logResults = function(str, title) {
  if(title!="") {
    var id = title; // todo - fix invalid ids
    $('#results').append('<div class="results-block"><h2 id="'+id+'">'+title+'</h2>'+str+'<div>');
    $('#results-anchors').append('<a href="#'+id+'">'+title+'</a><br>');
  } else {
    $('#results').append('<div class="results-block">' + str + '</div>');
  }
};

// dedupeArray()
// @array - an array to remove duplicate values from
// warning: all array elements will be cast to strings when making comparisons
var dedupeArray = function(array) {
	var seen = {};
  return array.filter(function(item) {
  	return seen.hasOwnProperty(item) ? false : (seen[item] = true);
  });
};


/**
  from https://github.com/mjackson/mjijackson.github.com/blob/master/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript.txt
     * Converts an RGB color value to HSL. Conversion formula
     * adapted from http://en.wikipedia.org/wiki/HSL_color_space.
     * Assumes r, g, and b are contained in the set [0, 255] and
     * returns h, s, and l in the set [0, 1].
     *
     * @param   Number  r       The red color value
     * @param   Number  g       The green color value
     * @param   Number  b       The blue color value
     * @return  Array           The HSL representation
     */

var rgbToHsl = function(r, g, b){
        r /= 255, g /= 255, b /= 255;
        var max = Math.max(r, g, b), min = Math.min(r, g, b);
        var h, s, l = (max + min) / 2;

        if(max == min){
            h = s = 0; // achromatic
        }else{
            var d = max - min;
            s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
            switch(max){
                case r: h = (g - b) / d + (g < b ? 6 : 0); break;
                case g: h = (b - r) / d + 2; break;
                case b: h = (r - g) / d + 4; break;
            }
            h /= 6;
        }

        return [h, s, l];
};






// getWikipediaPageHTML()
// @title - string of titles of wikipedia pages to look up.
// @callback - callback function, passed one argument containing HTML string of wikipedia page content
var getWikipediaPageHTML = function(title, callback) {
    var url = "https://en.wikipedia.org/w/api.php?action=parse&page="+title+"&format=json&redirects&callback=?";
    $.getJSON(url, function (data) {
        callback(data.parse.text['*']);
    });
};

// getWikipediaPageRedirects()
// @titles - array of titles of wikipedia pages to look up redirect info for
// @callback - callback function, passed one argument containing objects indicating redirects and normalisation
var getWikipediaPageRedirects = function(titles, callback) {
	var maxTitlesPerRequest = 50;
	var totalRequests = Math.ceil(titles.length / maxTitlesPerRequest);
  var completedRequests = 0;
  var redirectMap = {};
  var onResponse = function(data) {
  	for(var i=0; i<data.query.redirects.length; i++) {
    	redirectMap[data.query.redirects[i].from] = data.query.redirects[i];
    }
 		completedRequests++;
    if(totalRequests == completedRequests) {
    	callback(redirectMap);
    }
  };
	for(var i=0; i<titles.length; i+=maxTitlesPerRequest) {
  	var titlesForRequest = titles.slice(i,i+maxTitlesPerRequest);
    var url = "https://en.wikipedia.org/w/api.php?action=query&titles="+titlesForRequest.join("|")+"&format=json&redirects&callback=?";
    $.getJSON(url, function(data) {
			onResponse(data);
    });
  }
};


// onColourScrapeComplete()
// @colours - array of colour objects
// @callback - callback function, passed one argument containing final post-processed colour array
var onColourScrapeComplete = function(colours, callback) {
  var filteredColours = [];
  var titles = [];
  for(var i=0; i<colours.length; i++) {
    if(colours[i].name=="") {
      continue;
    }
    filteredColours.push(colours[i]);
  	var linkTitle = colours[i].linkTitle;
  	if(linkTitle!="" && linkTitle.indexOf("/") == -1) {
      // TODO filter out all linkTitles containing invalid characters
      // examplem of bad data: /w/index.php?title=Xanadu_(colour)&action=edit&redlink=1
    	titles.push(linkTitle);
    }
  }
  var titlesDeduped = dedupeArray(titles);
  
  // get any redirects for all colours found
  getWikipediaPageRedirects(titlesDeduped,function(redirectMap) {
    
    // logResults(htmlLog(redirectMap),'Redirects');
    
    // replace links in colour onbject with redirected links where applicable
    for(var i=0; i<filteredColours.length; i++) {
      var plainLink = filteredColours[i].link.replace("/wiki/","");
      if(redirectMap.hasOwnProperty(plainLink)) {
        filteredColours[i].link = redirectMap[plainLink].to;
        if(redirectMap[plainLink].hasOwnProperty("tofragment")) {
          filteredColours[i].link += "#" + redirectMap[plainLink].tofragment;
        }
      }
    }
    
    // sort array of colours by name
    filteredColours.sort(function(a,b) {
      return (a.name > b.name) ? 1 : ((b.name > a.name) ? -1 : 0);
    });
    
    // finally call callback with final list of colours
    callback(filteredColours);
  });
};

// scrapeColourDataFromHTML()
// @html - string of raw HTML to scrape colour data from
// @colours - array to add found colour objects to
var scrapeColourDataFromHTML = function(html, colours){
	$('<div>'+html+'</div>').find('table:first > tbody > tr').each(function(){
  	var cells = $(this).children();
    var a = cells.eq(0).children();
    
    var link = a.attr('href') || "";
    var linkTitle = link.replace('/wiki/','').split('#')[0];
    var hex = cells.eq(1).text().replace('#','');
    var red = parseInt(hex.substr(0,2),16);
    var green = parseInt(hex.substr(2,2),16);
    var blue = parseInt(hex.substr(4,2),16);
    var hsl = rgbToHsl(red, green, blue);
    
    colours.push({
    	name: a.text(),
      link: link,
      linkTitle: linkTitle,
      hexString: hex,
      red: red,
      green: green,
      blue: blue,
      hue: hsl[0],
      saturation: hsl[1],
      lightness: hsl[2]
    });
  });
};

// scrapeColoursFromWikipedia()
// @pages - array of titles of pages to look for colours in
// @callback - callback function, passed single argument of array of colour objects
var scrapeColoursFromWikipedia = function(pages,callback) {
  var completed = 0;
  var colours = [];
  for(var p=0; p<pages.length; p++) {
    getWikipediaPageHTML(pages[p], function(html) {
      scrapeColourDataFromHTML(html, colours);
      completed++;
      if(completed==pages.length) {
        onColourScrapeComplete(colours, callback);
      }
    });
  }
};

//
// do it
//

scrapeColoursFromWikipedia(["List_of_colors:_A-F","List_of_colors:_G-M","List_of_colors:_N-Z"],function(colours) {
  
  // show coloured squares
  var squares = '';
  for(var i=0; i<colours.length; i++) {
    squares += '<div style="padding:3px; display:inline-block; background-color:#'+colours[i].hexString+'">'+colours[i].name+'</div>';
  }
  logResults(squares,'Colours');
  
  // show data
  logResults(htmlLog(colours),'Colours data');
  logResults(JSON.stringify(colours),'Colours JSON');
  
});
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console