Javascript Help For Chrome Console? Clicking All links website - javascript

this website wants me to click ALL the links on the page (there's about 2000).
This is the code for each link:
UNFOLLOW
the ID changes all the time depending on the link.
Is it possible to supply me the javascript code to write into the console of google chrome to click ALL the "a" tags with the class of "unfollow" at once? thanks :)

How about?
var links = document.querySelectorAll("a.unfollow[href=#]");
for (var i = 0; i < links.length; ++i) {
links[i].click();
}

Sure is:
[].forEach.call(document.querySelectorAll('a.unfollow'), function (link){
link.click();
});
Demo
You could do it using a plain for loop as well, but I just found it faster to do it this way.

Thanks I used chat room this code works easy: $('a.unfollow').click()

Related

How to use JavaScript to click a div like button on an HTML page?

I'm currently trying to automate a game called Lyrics Training (https://www.lyricstraining.com/) and I am able to get the words that are from the button through some other code but I am currently struggling with clicking the "button" because it is for some reason classified as a in the HTML code. I was wondering whether there was a way or a function that would allow me to click it so I could finish the automation? Thanks!
So far I have this code that would work if the button was an actual button:
let firstChoice = document.getElementsByClassName("slot s1")[0];
let secondChoice = document.getElementsByClassName("slot s2")[0];
let thirdChoice = document.getElementsByClassName("slot s3")[0];
let fourthChoice = document.getElementsByClassName("slot s4")[0];
// the click function isn't working
for(let i = 0; i < click_order.length; i++){
let word = click_order[i];
if(firstChoice.innerHTML === word){
firstChoice.click();
}else if(secondChoice.innerHTML === word){
secondChoice.click();
}else if(thirdChoice.innerHTML === word){
thirdChoice.click();
}else if(fourthChoice.innerHTML === word){
fourthChoice.click();
}
}
This is how the "buttons" look like in the HTML code of the website:
Any help would be appreciated! Thanks!
If I am not misinterpreting anything, then it sounds like you are trying to automate a game on some website. I'm not sure exactly how they coded the game, but there is a chance the code doesn't actually listen for "click" events.
It seems like your code only works if the website's code responds to click events such as:
div.addEventListener("click", somefunction);
//or
btn.addEventListener("click", somefunction);
In this case, your code would still work on divs.
However, they could be responding to mousedown events:
div.addEventListener("mousedown", somefunction);
If this is the case, you might want to read up on invoking specific events: https://developer.mozilla.org/en-US/docs/Web/Events/Creating_and_triggering_events
But please keep in mind that many websites have code preventing this stuff.
Give the button div element an attribute of role="button". This should work fine.

Javascript code for addEventListener not working!? (open enlarged img when clicked)

Whenever an image is clicked, the image should be displayed alone in a new window. It should be done completely in javascript (so no onclick functions in the HTML should be used) and with no jquery. I´ve done some research and have managed to get this far:
Javascript
var img = document.getElementsByTagName("img");
for(var i=0; i < img.length; i++) {
img[i].addEventListener("click", enlarge);
}
function enlarge() {
window.open(this.src);
}
Unfortunately, nothing happens when any image is clicked and I can´t figure out why. Can anyone help me solve this problem of mine?
Thank you in advance!
your code should work fine check https://jsfiddle.net/3hgkaxgv/1/
The possible causes for your issue are:
Either browser compatibility or you already have something attached or catching click event.
Edit:
try the new changes if click registered was logged then you probably have popup disabled or something like that.
Try this instead of your code.
var img = document.getElementsByTagName("img");
for(var i=0; i < img.length; i++) {
img[i].onclick = enlarge;
}
function enlarge() {
console.log('click registered ');
window.open(this.src);
}
I found the solution!
The reason no clicks registrered (which turned out to be the issue as to why my code didn´t work for me) was because I put the <script src="inlamning6.js" type="text/javascript"> </script> first in the HTML-document, namely right after the <body> tag. It should be last in the HTML-document, right before the <body>tag is closed. I moved it accordingly, and everything works fine now!
I thought that this could be valuable information if anyone else ever gets stuck on the same problem as I did here.

clearing a link with javascript only works for the first link

Found a bit of javascript that clears the link to current page. So far so good. It worked until there were more than one link.
/*
CLCP v2.1 Clear Links to Current Page
Jonathan Snook
This code is offered unto the public domain
http://www.snook.ca/jonathan/
*/
window.onload = clearCurrentLink;
setTimeout("clearCurrentLink()",50);
function clearCurrentLink(){
var a = document.getElementsByTagName("A");
for(var i=0;i<a.length;i++)
if(a[i].href == window.location.href.split("#")[0])
removeNode(a[i]);
}
function removeNode(n){
if(n.hasChildNodes())
for(var i=0;i<n.childNodes.length;i++)
n.parentNode.insertBefore(n.childNodes[i].cloneNode(true),n);
n.parentNode.removeChild(n);
}
Thing is I can't figure out why it only clears the first link it finds.
I'm generating pages through node / expresss / jade / stylus. Any other ides on how to eliminate links to the current page?
Thanks!
The loop in clearCurrentLink should go in reverse order.

Activating a link on rollover?

Is it possible whit a simple script to activate a link just by a rollover without click?
You can do this:
<a onmouseover="window.location = this.href" href="http://google.com">test</a>
But use it wisely. No one will be expecting it.
May I ask why you're trying to do this?
I suppose by activate you mean follow. This may well work on your tag
onmouseover="window.location=event.target.href"
It's possible some browsers will block the attempt to do that though.
Using some unobtrusive javascript it can be achieved fairly easily.
var link = document.getElementById("link");
link.onmouseover = function(){
window.location.href = this.href;
};
Simple jsfiddle example.

Retrieve current focused/mouse-overed hyperlink URL

I'm working on an extension that I need to find a way to catch the current focused link.
When we hit TAB Key, or mouse over a hyperlink, we can see in the status bar (right side of the address bar for firefox 4+) the URL of that link has been shown.
How do you capture this URL with Javascript in Add-on online builder? how do I store it into a variable and whenever the focused link is changed, the variable value will be updated accordingly? I searched internet for hours and so far found this function called Document.activeElement.href ?? But I'm not sure that's what I need and if it is, how do I use it?
Please help!
Thanks !!!
This should do the trick:
<html><body>
link 1
link 2
<div id="output"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script>
var handler = function() {
jQuery('#output').text( jQuery(this).attr('href') );
};
jQuery('a').focus(handler).mouseover(handler);
</script>
</body></html>
Let me know if you don't want to use jQuery, and I'll re-write my answer.
The variable window.XULBrowserWindow.overLink happens to contain the current hovered URL as shown in the status bar but it doesn't save the actual element being hovered.
http://jsfiddle.net/DmX5j/6/
var links = document.getElementsByTagName('a'),
linkDisplay = document.getElementById('currentLink'),
currentLink;
for(var i =0; i < links.length; i++){
links[i].onfocus = function(){updateLink(this.href)};
links[i].onmouseover = function(){updateLink(this.href)};
}
function updateLink(link){
currentLink = link;
linkDisplay.innerHTML = currentLink;
}
pure JS way. Not sure if this is what you are looking for or not, basically on focus or mouseover the current link is updated.
you will need to put event handlers on all links of the interest, this is quite easy using jQuery, when the event trigger you can capture the href attribute and process accordingly

Categories

Resources