First of all, let's talk about the closed ecosystem of WeChat. Apart from opening the API to Sogou, a search engine that can search for WeChat official accounts, there are almost no other APIs available for use. Although this is for security reasons, it makes it difficult to implement some interesting features, such as a chatbot. And some practical features are not implemented in the official garbage client.
Requirements#
Using the web version of WeChat on Chrome, utilize the built-in console and JavaScript to achieve automatic and cyclic message sending.
Construction#
First of all, I want to declare that I did not have any prior knowledge of JavaScript. Everything was achieved step by step through the effective use of search engines. So, if any experts who read this article find anything inappropriate, please feel free to point it out in the comments.
View Page Source
First, of course, log in to the web version of WeChat, open the F12 developer tools, and try sending a few messages. Observe the webpage activity in the network tab. Then, I found the following HTML code:
<pre id="editArea" contenteditable-directive="" mm-paste="" class="flex edit_area ng-isolate-scope ng-pristine ng-valid" contenteditable="true" ng-blur="editAreaBlur($event)" ng-model="editAreaCtn" ng-click="editAreaClick($event)" ng-keyup="editAreaKeyup($event)" ng-keydown="editAreaKeydown($event)"></pre>
However, it didn't work...
Google Search
Then, I searched with the keywords "WeChat", "send message", "Chrome", "console", and "JavaScript". Finally, I found this article->Using JavaScript script to achieve scheduled message sending on WeChat, which matched the keywords. I opened it and found that it was exactly what I wanted.
Code Modification
// Monday to Friday: 6:50 AM remind the other party to wake up, 9:30 PM remind the other party to return to the dormitory
var appElement = document.querySelector('[ng-controller=chatSenderController]');
var $scope = angular.element(appElement).scope();
setInterval(function(){
var localTime = new Date();
if(localTime.getDay() < 6){ // Not the weekend
var localTimeString = localTime.toLocaleTimeString();
if(localTimeString.indexOf('6:49:00 AM') === 0){
$scope.editAreaCtn = "It's 6:50, time to wake up!";
$scope.sendTextMessage();
}else if(localTimeString.indexOf('6:54:00 AM') === 0){
$scope.editAreaCtn = "It's a new day, good luck!";
$scope.sendTextMessage();
}else if(localTimeString.indexOf('9:28:00 PM') === 0){
$scope.editAreaCtn = "It's almost 9:30, time to go back!";
$scope.sendTextMessage();
}
}
},1000);
In this article, there are additional checks before sending WeChat messages, but I don't need them. I just need to send messages without thinking (oh, I also need to set an interval time, otherwise it will become spamming). Finally, I modified it like this:
var appElement = document.querySelector('[ng-controller=chatSenderController]');
var $scope = angular.element(appElement).scope();
setInterval(function(){
$scope.editAreaCtn = "Message content";
$scope.sendTextMessage();
},1000);
Then, I ran this piece of code in the console, and oh no, it was too fast. I quickly stopped it and found that Chrome was completely focused on sending messages and froze... I had no choice but to kill the Chrome process.
Adding Sleep and Final Result
Then I remembered that Python has a sleep function, so I searched for it and found a homemade method for JavaScript sleep (JavaScript itself does not support sleep).
Finally, I modified the code again, combined it with a for loop for an infinite loop, and achieved the final result:
var appElement = document.querySelector('[ng-controller=chatSenderController]');
var $scope = angular.element(appElement).scope();
async function test() {
$scope.editAreaCtn = "Message content";
$scope.sendTextMessage();
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
i = 0
for (; ; i++) {
test()
await sleep(1000000)
}
So, all you need to do is log in to the web version of WeChat, locate the chat window where you want to send messages, and run the code!
Continued
After finishing the previous article, a problem arose. Someone started making fun of me for having encrypted code. So, I made some changes according to their request and added the functionality to randomly select an interval time between 10 and 15 minutes. Here is the code:
var appElement = document.querySelector('[ng-controller=chatSenderController]');
var $scope = angular.element(appElement).scope();
var n = 10;
var m = 15;
// Import sleep
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
// Send message
async function test() {
$scope.editAreaCtn = "Message content";
$scope.sendTextMessage();
}
// Generate a random number between n and m
function rd(n,m){
var c = m-n+1;
return Math.floor(Math.random() * c + n);
}
// Infinite loop
while(true){
test();
t = rd(n,m)
console.log(t)
await sleep(t * 10000);
}
End of today's article.