A `setInterval` Script Deleted 14 Pages of Social Posts While Its Author Played Mahjong Link
Background
A restless day. I glanced at the Juejin short posts I'd posted before and felt they read like the cringey Qzone logs I wrote back in school.
So I thought I'd delete some — otherwise I'd be exposing my true nature of loving to mess around.
Then I opened "Creator Center" - "Content Management" - "Short Post Management" and patiently deleted them one by one…
Two minutes passed. I looked at the 14 remaining pages of short posts…
I was numb.
Analysis
As a programmer, I felt I couldn't keep doing this…
So I did the following:
- F12 to inspect the page, found the HTML element for "Delete"
- Clicked the "Delete" button, saw the confirmation popup, and incidentally found which HTML element the "Confirm" button was
- Analysis done, I had a plan in mind.
Execution
The next steps were simple. I just needed to do this, then that, and finally this — and it should work.
Haha, no more nonsense. Let's first do this in the browser:
// Try running this in the console
document.getElementsByClassName('byte-dropdown-item')[0].click();
Then, continue like this:
// After the popup appears, run the following code
document.getElementsByClassName('btn-confirm')[0].click();
Finally, the short post disappeared.
Optimization
Two lines of code saved me half a minute of clicking, but it was still tiring.
As a programmer, I had to optimize.
So I wrote a function:
function deleteShortMsg() {
const msgDeleteElements = document.getElementsByClassName('byte-dropdown-item');
if (msgDeleteElements.length > 0) {
msgDeleteElements[0].click()
setTimeout(() => {
const confirmBtnElements = document.getElementsByClassName('btn-confirm')
if (confirmBtnElements.length > 0) {
confirmBtnElements[0].click()
}
}, 1000)
}
}
// Invoke
setInterval(deleteShortMsg, 2000)
When I hit Enter, I sipped water and watched the short posts disappear one by one before my eyes.
Afterwards
After a few more rounds of Mahjong Link, my short posts were as clean as my pockets…
Warning
In the end, I just want to say: Don't try it! Don't try it! Don't try it!
Summary
Of course, this isn't the first time I've done something like this.
One day before, I accidentally opened my own Qzone, scrolled through those unbearable texts, and had a similar idea.
So this was me transferring the experience over; the principle is pretty much the same.
I'll still briefly share my thought process and ideas here:
- When we have similar needs, we can first summarize the core parts of these repetitive steps. For example, do we need to click a certain element, then a confirmation popup appears, then click another element to achieve our goal. In summary, abstract the process we want to simulate as much as possible, and the more precise the better.
- Then locate the elements for these key steps. Since it's an HTML page, consider using methods like "getElementsByClassName". Note that you need to analyze the operations that trigger the element to enter the next step; some are clicks, some are mouse movements. For example, Juejin's short posts use the "mouseenter" event, so you need to analyze patiently.
- Finally, this can not only help us do some simple repetitive operations, but is also very helpful for basic crawler simulation. Many simple crawlers can use a headless browser to simulate human operations through code, scrape the data, and finally generate files for our use.
With that, a little life hack shared with everyone. I hope you never need to use it. Haha.
Top 1 from juejin.cn, machine-translated. The original thread is authoritative.
Good, not bad, write more