class_name = document.getElementsByClassName('image');
const map = fn => x => Array.prototype.map.call(x, fn);
map(img => {
console.log(img);
img.addEventListener('mouseover', (e) => {
global = e.target.src;
calledsomething(global);
});
})(class_name);
I'm trying to make a simple chrome extension but Array.prototype.map.call
doesn't seems to be called. console.log(img)
doesn't display anything.
I tested my code using getElementsByTagName('img')
instead of getElementsByClassName
and it works.
What might be the problem?
EDIT: The whole code:
var isHovered = false;
var global;
const class_name = document.getElementsByClassName('photo_activity_item__img_wrapper');
const map = fn => x => Array.prototype.map.call(x, fn);
console.log(class_name);
map(img => {
img.addEventListener('mouseover', (e) => {
global = e.target.src;
console.log(global);
hoveredBox();
});
img.addEventListener('mouseleave', (e) => {
isHovered = false;
});
})(class_name);
document.addEventListener('keypress', keyDown);
function hoveredBox() {
isHovered = true;
}
function keyDown(event) {
if (!isHovered) return;
var key = event.keyCode;
if (key === 115) {
saveFile(global);
}
}
// Download a file form a url.
function saveFile(url) {
// Get file name from url.
filename = url;
var xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = function () {
var a = document.createElement('a');
a.href = window.URL.createObjectURL(xhr.response); // xhr.response is a blob
console.log(a.href);
console.log(xhr.response);
a.download = filename; // Set the file name.
a.style.display = 'none';
document.body.appendChild(a);
a.click();
delete a;
};
xhr.open('GET', url);
xhr.send();
}