r/javascript • • Mar 18 '19

What's your favorite, most useful javascript one-liner?

385 Upvotes

422 comments sorted by

View all comments

76

u/anlumo Mar 18 '19

EU citizen here. Whenever a site presents me with a tracking consent form that has >500 checkboxes to click in order to tell them to disable all tracking, I open up the dev console and do something like this:

document.querySelectorAll('.whateverClassTheCheckboxHas').forEach(cb => cb.click());

6

u/BluudLust Mar 19 '19

Check out the extension "I do not care about cookies"

6

u/anlumo Mar 19 '19

If I'm reading the description correctly, that extension doesn't actually turn off the cookies, it just hides the popup.

2

u/BluudLust Mar 19 '19 edited Mar 19 '19

Enable firefox's built in "Do Not Track" features (it can block resources too now), use UBlock Origin, and Decentraleyes, and you've got everything you want. Still allows session cookies and everything for proper functionality, but prevents all ways to track you (3rd party stuff, and tracking via CDN) otherwise. Probably should use HTTPS Everywhere too as a safety precaution too.

5

u/anlumo Mar 19 '19

Google now tracks via the injected JavaScript, meaning that they’re no longer third party cookies and also not on the CDN. I'm sure others do as well.

2

u/[deleted] Mar 19 '19

NodeLists do not have the Array functions on their prototype.

[].forEach.call(document.querySelectorAll('.whateverClassTheCheckboxHas'), cb => cb.click());

33

u/AwesomeInPerson Mar 19 '19

NodeList actually implements forEach(), with decent browser support. :)

12

u/anlumo Mar 19 '19

Hmm map doesn't work here in Chrome, but forEach does.

17

u/[deleted] Mar 19 '19

Oh, hey. Looks like forEach has been added to the WebIDL spec for NodeList and other DOM iterables. Ain't that fancy. Time to go revise a whole bunch of code!

11

u/the_argus Mar 19 '19

Can also use Array.from(document...).forEach(...)

13

u/BabyLegsDeadpool Mar 19 '19

Or just do [...document.querySelectorAll(...)].forEach(...)

8

u/Sn34kyMofo Mar 19 '19

Or, if you're in Chrome, use $$(), which is a console method that returns the result of querySelectorAll() as an array instead of a nodelist! Very succinct and handy when futzing with the DOM in DevTools.

Example: $$('.some-class').forEach(...);

1

u/fucking_passwords Mar 19 '19

But, NodeList has forEach... if you example used map or reduce it would make more sense

1

u/Sn34kyMofo Mar 19 '19

Indeed. My reply was simply meant to contribute, not dispute. Pick any other array-only method to use with my example instead. =)

0

u/enHello Mar 19 '19

Or [].slice.call(document.querySelectorAll(...)).map(node => node.foo)

1

u/alinnert Mar 19 '19

Yeah, this is the pre-ES6 version. I used it a lot when I had to. It's just not as clear about what it's doing.

1

u/enHello Mar 19 '19

Agreed.

0

u/[deleted] Mar 19 '19

[deleted]

1

u/[deleted] Mar 19 '19

It does not. The Chrome command line API comes with $, which is not available from browser code. Also, the CLA version of $ is equivalent to document.querySelector, not querySelectorAll.

3

u/[deleted] Mar 19 '19

Dev-tools is the context of the conversation

2

u/Sn34kyMofo Mar 19 '19

$$ is querySelectorAll in DevTools, except it returns the nodelist as an array, which is nice.

Example: $$('.ad-container').slice(-1)[0].remove();

1

u/shamshuipopo Mar 19 '19

Shouldn’t everything be opt in after gdpr?

3

u/anlumo Mar 19 '19

Yes, but I have seen more violations of that part than compliance.

1

u/100mcg Mar 19 '19

That's how I mass unfollowed everyone on my old Twitter, I hadn't used it in a few years and previously I had just followed a lot of random people because they had followed me first, ended up with 1000+ I didn't care about, I wanted to start over without losing the follower count so I just iterated over and clicked all the unfollow buttons

→ More replies (2)