Answer the question
In order to leave comments, you need to log in
How to execute commands one by one in google chrome extension?
I'm writing an extension for Crome. I need to first get the number of pages, and then get the page url. I want to do it consistently. You need to wait, first the execution of the code, then move on to the execution of the next code. How can this be done? That is, work consistently.
document.addEventListener('DOMContentLoaded', function() {
var checkPageButton = document.getElementById('collection');
var name = '';
var showSettings = document.getElementById('showSettings');
showSettings.addEventListener('click', function() {
displaySettings.run();
}, false);
displaySettings.run();
}, false);
var displaySettings = (function (){
let display = false;
return {
run: function (){
let displayStyle = (!display) ? "none": null;
document.getElementById('settings').style.display = displayStyle;
display = !display;
}
}
}());
var collection = (function (){
let pages = false;
let startUrl = false;
let arrAccount = [];
return {
run: function (){
/** ждем получение количество страниц**/
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.action == "pages") { /** обробатываем количество страниц **/
collection.setPagesCount(request.pages);
chrome.tabs.executeScript(null,{ code: `console.log( 'pagination - ${request.pages}')`});
if(collection.getPagesCount() > 2) {
chrome.tabs.getSelected(null, function(tab) {
collection.getAccaunts(tab.url,collection.getPagesCount()) });
}
}});
/** здесь получить URL страницы можно было бы вставить в верхнюю функцию, но на могу понять как сделать что бы код работал последовательно, а не ассинхронно.**/
collection.paginationCount();
},
paginationCount: function(){
chrome.tabs.executeScript(null, { code: `
(function (){
var timesRun = 0;
var checkExist = setInterval(function() {
timesRun += 1;
window.scrollTo(0,document.body.scrollHeight);
if (timesRun > 10) {
clearInterval(checkExist);
}
var elementExists = document.getElementsByClassName('artdeco-pagination__indicator artdeco-pagination__indicator--number')[document.getElementsByClassName('artdeco-pagination__indicator artdeco-pagination__indicator--number').length - 1];
if (elementExists !== undefined) {
clearInterval(checkExist);
chrome.runtime.sendMessage({action: "pages", pages: document.getElementsByClassName('artdeco-pagination__indicator artdeco-pagination__indicator--number').length })
}
}, 50);}());
`});
},
startUrlScript: function(){
chrome.tabs.executeScript(null, { code: `
(function (){
var startUrl = window.location.href;
if (startUrl !== undefined) {
chrome.runtime.sendMessage({action: "startUrlInfo", startUrl: startUrl })
}}())
`});
},
getPagesCount: function(){ return pages;},
setPagesCount: function(data){ pages = data;},
getStartUrl: function(){ return window.location.href;},
setStartUrl: function(data){ pages = data;},
getAccaunts: function(url, pages){
for(page = 1; page <= pages; page++){
chrome.tabs.executeScript(null,{ code: `let data = []; let arr = document.getElementsByClassName('search-result--person');
for (let item of arr) {
data.push(item.getElementsByTagName('a')[0].href);
}
console.log( data)
`}, function(result){
console.log(result);
}
},
goNextPage: function() {
document.location.href = url.value;
},
}
}());
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question