我想使用普通 Javascript 在 Linkedin 上发送自动消息,但它不起作用

问题描述 投票:0回答:1

我正在使用一个程序,该程序会按预期执行所有操作,直到它必须将消息文本插入文本框内。

正常工作后,按下发送按钮并进行其余所有操作。

问题出在 PasteNote 函数上。

 

Linkedin = {
    config: {
        scrollDelay: 2000,
        actionDelay: 3000,
        nextPageDelay: 5000,
        // set to -1 for no limit
        maxRequests: -1,
        totalRequestsSent: 0,
        // set to false to skip adding note in invites
        // addNote: true,
        note: "Dear {{name}}, thank you for sharing your professional network."
    },
    //The init function is called when the script is initialized.
    //and it sets a delay before calling the scrollBottom function. 
    init: function (data, config) {
        console.info("INFO: script initialized on the page...");
        console.debug("DEBUG: scrolling to bottom in " + config.scrollDelay + " ms");
        setTimeout(() => this.scrollBottom(data, config), config.actionDelay);
    },
    //The scrollBottom function uses the scrollTo method to smoothly scroll the page to the bottom. 
    //After a delay, it calls the scrollTop function.
    scrollBottom: function (data, config) {
        window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' });
        console.debug("DEBUG: scrolling to top in " + config.scrollDelay + " ms");
        setTimeout(() => this.scrollTop(data, config), config.scrollDelay);
    },
    //The scrollTop function, scrolls the page back to the top.
    scrollTop: function (data, config) {
        window.scrollTo({ top: 0, behavior: 'smooth' });
        console.debug("DEBUG: inspecting elements in " + config.scrollDelay + " ms");
        //The scrollTop function then sets a delay before calling the inspect function
        setTimeout(() => this.inspect(data, config), config.scrollDelay);
    },
    //The inspect function looks for elements on the page with the text "Message" 
    //and stores them in an array called pageButtons.
    inspect: function (data, config) {
        var totalRows = this.totalRows();
        console.debug("DEBUG: total search results found on page are " + totalRows);
        if (totalRows >= 0) {
            this.compile(data, config);
        } else {
            console.warn("WARN: end of search results!");
            this.complete(config);
        }
    },
    compile: function (data, config) {
        var elements = document.querySelectorAll('button');
        data.pageButtons = [...elements].filter(function (element) {
            return element.textContent.trim() === "Message";
        });
        //If this array (pageButtons) is empty, the script moves to the next page by calling the nextPage function. 
        //If the array is not empty, the script begins sending messages by calling the sendMessages function.
        if (!data.pageButtons || data.pageButtons.length === 0) {
            console.warn("ERROR: no message buttons found on page!");
            console.info("INFO: moving to next page...");
            setTimeout(() => { this.nextPage(config) }, config.nextPageDelay);
        } else {
            data.pageButtonTotal = data.pageButtons.length;
            console.info("INFO: " + data.pageButtonTotal + " message buttons found");
            data.pageButtonIndex = 0;
            var names = document.getElementsByClassName("entity-result__title-text");
            names = [...names].filter(function (element) {
                return element.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.textContent.includes("Message\n");
            });
            data.connectNames = [...names].map(function (element) {
                return element.innerText.split(" ")[0];
            });
            console.debug("DEBUG: starting to send messages in " + config.actionDelay + " ms");
            setTimeout(() => { this.sendMessages(data, config) }, config.actionDelay);
        }
    },
    //The sendMessages function clicks the button at the current index of the pageButtons array, 
    sendMessages: function (data, config) {
        console.debug("remaining requests " + config.maxRequests);
        if (config.maxRequests == 0) {
            console.info("INFO: max requests reached for the script run!");
            this.complete(config);
        } else {
            console.debug('DEBUG: sending invite to ' + (data.pageButtonIndex + 1) + ' out of ' + data.pageButtonTotal);
            var button = data.pageButtons[data.pageButtonIndex];
            button.click();
            //and if the addNote configuration is set to true, it adds a note to the invite using the clickAddNote function.
            // clicking Add a note in popup, if present
            console.debug("DEBUG: pasting Message in popup, in " + config.actionDelay + " ms");
            setTimeout(() => this.pasteNote(data, config), config.actionDelay);

            // clicking done in popup, if present
            // console.debug("DEBUG: clicking done in popup, in " + config.actionDelay + " ms");
            // setTimeout(() => this.clickDone(data, config), config.actionDelay);

    }},
   
    //The addNote function adds the specified note to the invite by replacing the string "{{name}}" 
    //with the name of the recipient and then setting the value of the text area to the modified note. 
    //It then clicks the "Send" button to send the invite.

    pasteNote: function (data, config) {
        console.log("start of pasteNote" + noteTextBox);
        //noteTextBox = document.getElementByClass("msg-form__msg-content-container");
        // we select the space where we want to insert the note
        noteTextBox = document.querySelector("[class^='msg-form__msg-content-container--scrollable']");
        console.log(noteTextBox.value);
        noteTextBox.value = config.note.replace("{{name}}", data.connectNames[data.pageButtonIndex]);
        noteTextBox.dispatchEvent(new Event('input', {
            bubbles: true
        }));
        window.onload = noteTextBox.value;
        console.log("end of pasteNote" + noteTextBox.value);

        console.debug("DEBUG: clicking send in popup, if present, in " + config.actionDelay + " ms");
        setTimeout(() => this.clickDone(data, config), config.actionDelay);
    },
    clickDone: function (data, config) {
        var buttons = document.querySelectorAll('button');
        var doneButton = Array.prototype.filter.call(buttons, function (el) {
            return el.textContent.trim() === 'Send';
        });
        // Click the first send button
        if (doneButton && doneButton[0]) {
            console.debug("DEBUG: clicking send button to close popup");
            doneButton[0].click();
        } else {
            console.debug("DEBUG: send button not found, clicking close on the popup in " + config.actionDelay);
        }
        setTimeout(() => this.clickClose(data, config), config.actionDelay);
    },
    clickClose: function (data, config) {
        var closeButton = document.getElementsByClassName('artdeco-modal__dismiss artdeco-button artdeco-button--circle artdeco-button--muted artdeco-button--2 artdeco-button--tertiary ember-view');
        if (closeButton && closeButton[0]) {
            closeButton[0].click();
        }
        console.info('INFO: invite sent to ' + (data.pageButtonIndex + 1) + ' out of ' + data.pageButtonTotal);
        config.maxRequests--;
        config.totalRequestsSent++;
        //After sending the invite, the function increments the pageButtonIndex and checks 
        //if it is less than the total number of buttons. 
        //If it is, the function calls itself again with the updated pageButtonIndex.
        //If not, it calls the complete function.
        if (data.pageButtonIndex === (data.pageButtonTotal - 1)) {
            console.debug("DEBUG: all connections for the page done, going to next page in " + config.actionDelay + " ms");
            setTimeout(() => this.nextPage(config), config.actionDelay);
        } else {
            data.pageButtonIndex++;
            console.debug("DEBUG: sending next invite in " + config.actionDelay + " ms");
            setTimeout(() => this.sendMessages(data, config), config.actionDelay);
        }
    },

    nextPage: function (config) {
        var pagerButton = document.getElementsByClassName('artdeco-pagination__button--next');
        if (!pagerButton || pagerButton.length === 0 || pagerButton[0].hasAttribute('disabled')) {
            console.info("INFO: no next page button found!");
            return this.complete(config);
        }
        console.info("INFO: Going to next page...");
        pagerButton[0].children[0].click();
        setTimeout(() => this.init({}, config), config.nextPageDelay);
    },
    //Finally, the complete function is called when the script has finished running 
    //or when the maximum number of requests has been reached. 
    //It logs a message to the console indicating that the script has completed.
    complete: function (config) {
        console.info('INFO: script completed after sending ' + config.totalRequestsSent + ' messages');
    },
    totalRows: function () {
        var search_results = document.getElementsByClassName('search-result');
        if (search_results && search_results.length != 0) {
            return search_results.length;
        } else {
            return 0;
        }
    }
}

Linkedin.init({}, Linkedin.config);




它遵循控制台的输出

NFO: script initialized on the page... debugger eval code:18:17
DEBUG: scrolling to bottom in 2000 ms debugger eval code:19:17
undefined
DEBUG: scrolling to top in 2000 ms debugger eval code:26:17
DEBUG: inspecting elements in 2000 ms debugger eval code:32:17
DEBUG: total search results found on page are 0 debugger eval code:40:17
INFO: 1 message buttons found debugger eval code:61:21
DEBUG: starting to send messages in 3000 ms debugger eval code:70:21
remaining requests -1 debugger eval code:76:17
DEBUG: sending invite to 1 out of 1 debugger eval code:81:21
DEBUG: pasting Message in popup, in 3000 ms debugger eval code:86:21
start of pasteNote[object HTMLDivElement] debugger eval code:100:17
undefined debugger eval code:104:17
end of pasteNote Dear Valerio, I'm Valerio, thank you for sharing your professional network. debugger eval code:110:17
DEBUG: clicking send in popup, if present, in 3000 ms debugger eval code:112:17
DEBUG: clicking send button to close popup debugger eval code:122:21
INFO: invite sent to 1 out of 1 debugger eval code:134:17
DEBUG: all connections for the page done, going to next page in 3000 ms debugger eval code:142:21
INFO: no next page button found! debugger eval code:154:21
INFO: script completed after sending 1 messages debugger eval code:165:17

我尝试更改querySelector。此外,控制台日志值是预期的,但它不会出现在文本框中。

javascript linkedin-api messagebox browser-automation
1个回答
0
投票

var el_msg_form = document.getElementsByClassName("msg-form__contenteditable")[0]; 
var el_name = document.getElementsByClassName("profile-card-one-to-one__profile-link")[0];
var el_placeholder = document.getElementsByClassName("msg-form__placeholder")[0];
var first_name = el_name.innerHTML.trim().split(" ")[0];
var greeting = "Hi " + first_name + ",\n\n";
document.execCommand('insertText', false, greeting);
el_msg_form.focus();
el_placeholder.remove();
document.close();

© www.soinside.com 2019 - 2024. All rights reserved.