Skip to content

Commit

Permalink
feat(background): Tweak the executeScripts helper
Browse files Browse the repository at this point in the history
This commit adds a comment to for this method, formats the code using Prettier and replaces the for loop with a for...of statement
  • Loading branch information
ERosendo committed Jul 18, 2023
1 parent e20b42d commit e2ee463
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,31 @@ function showNotificationTab(details){
}
}

function executeScripts(tabId, injectDetailsArray)
{
function createCallback(tabId, injectDetails, innerCallback) {
return function () {
chrome.tabs.executeScript(tabId, injectDetails, innerCallback);
};
}
function executeScripts(tabId, injectDetailsArray) {
// This method uses programmatic injection to load content scripts
// and waits until the helper finishes injecting a script before
// loading the succeeding one into the active tab.
//
// This helper uses the callback parameter of the executeScript and a
// recursive approach to define the sequence of operations that
// guarantees the files are loaded in the same order they are listed
// in the injectDetailsArray parameter.
//
// You can read more on this at this [PR](https://github.com/freelawproject/recap-chrome/pull/340).

var callback = null;
function createCallback(tabId, injectDetails, innerCallback) {
return function () {
chrome.tabs.executeScript(tabId, injectDetails, innerCallback);
};
}

for (var i = injectDetailsArray.length - 1; i >= 0; --i)
callback = createCallback(tabId, injectDetailsArray[i], callback);
var callback = null;

for (var fileDetails of injectDetailsArray.reverse()) {
callback = createCallback(tabId, fileDetails, callback);
}

if (callback !== null)
callback(); // execute outermost function
if (callback !== null) callback(); // execute outermost function
}

async function injectContentScript(tabId, status, url) {
Expand Down

0 comments on commit e2ee463

Please sign in to comment.