Created
August 19, 2018 16:50
-
-
Save evanre/d1fe073924a948f06433253bda158c33 to your computer and use it in GitHub Desktop.
Search for nice and short domains script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Search for nice and short domains. | |
* Use for example a NameCheap | |
*/ | |
// Flatten array. Ex.: ['aa', 'ab', ['ac', 'ad']] => ['aa', 'ab', 'ac', 'ad'] | |
const flatten = arr => [].concat.apply([], arr); | |
// Remove non-unique items from given array | |
const uniq = a => [...new Set(a)]; | |
// Split a very long array on chunked pieces | |
const chunkArray = (arr, chunk) => { | |
const results = []; | |
while (arr.length) { | |
results.push(arr.splice(0, chunk)); | |
} | |
return results; | |
}; | |
// Cartesian Product implementation Javascript Implementation | |
// Source: https://eddmann.com/posts/cartesian-product-in-javascript/ | |
const cartesian = (...sets) => sets.reduce((prev, curr) => flatten(prev.map(x => curr.map(y => [...x, y]))), [[]]); | |
// Get whois fetch wrapper. Working api needed! | |
// Not used on current script | |
const getWhois = domain => { | |
let header = new Headers({ | |
'Access-Control-Allow-Origin': '*', | |
'Content-Type': 'multipart/form-data' | |
}); | |
fetch(`http://dotnul.com/api/whois/${domain}`, { // not working, need to find a Whois API | |
method: 'GET', | |
mode: 'no-cors', | |
headers: { | |
'Access-Control-Allow-Origin': '' | |
}, | |
}) | |
.then((response) => { | |
console.log(response.whois); | |
if (response.whois.indexOf('DOMAIN NOT FOUND') > 0) { | |
return response; | |
} | |
// const error = new Error(response.statusText); | |
// error.response = response; | |
// throw error; | |
}) | |
.then(response => response.json()) | |
.then((data) => { | |
console.log(data); | |
}); | |
}; | |
// Generate all possible variants from the given arrays. | |
const getPossibleDomains = (zone, length, filterByZone = false) => { | |
// const alphabet = 'abcd'.split(''); // shorten version for developing | |
const alphabet = 'abcdefghijklmnopqrstuvwxyz'.split(''); // alphabet | |
// As we use only one array - an alphabet, we need to create an array with N items of alphabets | |
// N - length of generated items | |
const sets = Array.from({length}, () => alphabet); | |
// Generate all possible combinations, Each item has a @length symbols quantity | |
let str = cartesian(...sets); | |
// Optional. | |
// Filter only entries that includes given domain zone letters. Example: lmel.me, ecom.com etc | |
// Filtering algorithm: | |
// Each item, remove non-unique letters from it | |
// Filter this string if it contains all symbols from domain zone. You can specify only 1 or 2 letters you want to match. Just replace zone.length on any number | |
if (filterByZone) { | |
str = str.filter(string => uniq(string).filter(char => zone.indexOf(char) >= 0).length === zone.length); | |
} | |
// Join result into string and concatenate a domain zone to get an array of valid domains | |
return str.map(arr => `${arr.join('')}.${zone}`); | |
}; | |
// Output list on the page | |
const buildLists = (entries, chunk) => { | |
const container = document.body; // get any element on the page | |
const totalEntries = document.createElement("p"); // Create paragraph for total information; | |
totalEntries.appendChild(document.createTextNode(`Total entries: ${entries.length}\n`)); // add total entries info | |
// Split a very long array on chunked pieces, if NameCheap returns 404 error, try to reduce chunk size | |
entries = chunkArray(entries, chunk); | |
totalEntries.appendChild(document.createTextNode(`Chunked entries: ${entries.length}, chunk size: ${chunk}`)); // add chunked entries info | |
container.appendChild(totalEntries); // Put total info paragraph into a page | |
// Output links in different formats | |
for (let i = 0; i < entries.length; i++) { | |
// Create a texatea and put it into a pge | |
// const area = document.createElement("textarea"); | |
// area.setAttribute('onClick', 'this.select();'); | |
// area.appendChild(document.createTextNode(entries[i].join('\n'))); // Ech domain on a new line | |
// container.appendChild(area); | |
// Create and anchor to NameCheap lookup page with domains | |
const anchor = document.createElement('a'); | |
//const span = document.createElement('span'); | |
// generate escaped urls string for link string | |
anchor.setAttribute('href', `https://www.namecheap.com/domains/registration/results.aspx?domain=${entries[i].join('%0A')}&usetlds=no&type=bulk`); | |
anchor.setAttribute('rel', 'noopener'); | |
anchor.setAttribute('target', '_blank'); | |
//console.log( entries ); | |
anchor.appendChild(document.createTextNode(`link-${i}`)); | |
//span.appendChild(document.createTextNode(entries[i].join('; '))); | |
container.appendChild(anchor); | |
container.appendChild(document.createElement("br")); | |
//container.appendChild(span); | |
//container.appendChild(document.createElement("br")); | |
} | |
// Optional, if we created anchors to NameCheap, we can click each of the links through the loop. | |
// Delay is to prevent ban from NameCheap | |
// const anchors = container.querySelectorAll('a'); | |
// const clickDelay = 3000; // 3 seconds | |
// for (let j = 0; j < anchors.length; j++) { | |
// (function (j) { | |
// setTimeout(function () { | |
// // anchors[j].containerlick(); | |
// // console.log( 'hello' ); | |
// }, clickDelay*j); | |
// })(j); | |
// } | |
}; | |
const array = getPossibleDomains('pw', 3, true); // get all possible 4-symbol length name in '.me' domain zone | |
buildLists(array, 180); // Output links on the page |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment