Skip to content

Instantly share code, notes, and snippets.

@udovichenko
Last active February 5, 2019 20:44
Show Gist options
  • Save udovichenko/fd52cd4306ceb7201ad5 to your computer and use it in GitHub Desktop.
Save udovichenko/fd52cd4306ceb7201ad5 to your computer and use it in GitHub Desktop.
Allows to generate wget query for a custom selector on a page
var parentElement = '.gamma-decori'; // Родительский элемент
var elements = ['img']; // из каких элементов брать путь к картинкам?
var fileTypes = ['jpg', 'jpeg', 'png', 'gif', 'svg', 'bmp']; // какие разрешения нужны?
var selector = [];
elements.forEach(function(elem){
var href = (elem == 'a')
? 'href'
: 'src';
fileTypes.forEach(function(ext) {
selector.push( parentElement +' '+ elem + '[' + href + '$=".' + ext + '"]' );
});
});
selector = selector.join(',');
var nodeList = document.querySelectorAll( selector );
var wgetList = [];
// var counter = 0;
Array.prototype.forEach.call (nodeList, function (node) {
if (node.tagName == 'A') {
var href = node.getAttribute('href');
}
else if (node.tagName == 'IMG') {
var href = node.getAttribute('src');
} else
return true;
if (href.indexOf('//') == -1)
href = 'https://'+location.hostname+href;
var newItem = 'wget "' + href.replace(/\s/g, '%20') + '" -O ' + href.split('/').slice(-1);
if (wgetList.indexOf(newItem) == -1)
wgetList.push(newItem);
});
console.log(nodeList.length);
console.log(wgetList.length);
wgetList = wgetList.join(' && ');
console.log(wgetList);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment