javascript - Chrome Extension: How to make background wait executescript? -


i've started developing first google chrome extension, , experiencing issue. in background.js script each second call script.js, smth like:

script.js:

/* code */ if (condition1) {     settimeout(func1, 500);     result = 1; } else if (condition2) {     settimeout(func2, 4000);     result = 2; } else /*some code */  result 

background.js:

function func() {     chrome.tabs.executescript(null, {file: 'script.js'},         function (result) {             console.log(result[0]);         }     );          /* code using results of scipt.js */ };  var interval = null; function onclickhandler(info, tab) {     if (info.menuitemid == "addon") {         if (interval != null) {             clearinterval(interval);             interval = null;         } else {             interval = setinterval(func, 1000);         }     } };  chrome.contextmenus.onclicked.addlistener(onclickhandler);  chrome.runtime.oninstalled.addlistener(function() {     chrome.contextmenus.create({"title": "addon", "id": "addon"}); } 

so need call script each max(1 second, after previous executescript). should work in way:

1. if previous script.js finished , 1 second after previous      interval call passed, call new script.js 2. if previos script.js finished , 1 second after previous      interval call haven't passed, wait 1 second , call new script.js 3. if previous script.js haven't finished, wait until finish      , if 1 second passed, call new script.js 

thanks in advance help.

chrome.tabs.executescript callback expects synchronous result. logic asynchronous. in such cases have set proper inter-process communication.

background.js

chrome.tabs.executescript(null, {file: 'script.js'}); chrome.runtime.onmessage.addlistener(function(request, sender, sendresponse) {      if (request.action === 'done') {          console.log(request.result);          // code          sendresponse({ action: 'next', arg: 2 });      }       // uncomment if code asynchronous      // return true; });  // optional // chrome.tabs.query({active: true, currentwindow: true}, function(tabs) { //     chrome.tabs.sendmessage(tabs[0].id, { action: 'next', arg: 1 }); // }); 

script.js

function func1() {     // code     var result = 1;     done(result); } function func1(2) {     // code     var result = 2     done(result); }  function runaction(which) {     switch (which) {         case 1: settimeout(func1, 500); break;         case 2: settimeout(func2, 500); break;     } }  function done(result) {     var msg = { action: 'done', result: result }};     chrome.runtime.sendmessage(msg, function(response) {         if (response.action === 'next') {             runaction(response.arg);         }     }); }  // start runaction(1); 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -