Here's the script:
function runScripts() { if (arguments.length === 0) return; chrome.tabs.executeScript(null, { file: arguments[0] }, function() { arguments.shift(); runScripts.apply(null, arguments); }); }
It doesn't work because arguments
is not actually an array, it's just array-like. So how can I "shift" it or hack off the first element so that I can apply this function recursively?
Shift is a builtin command in bash which after getting executed, shifts/move the command line arguments to one position left. The first argument is lost after using shift command. This command takes only one integer as an argument.
The shift command changes the values of the batch parameters %0 through %9 by copying each parameter into the previous one—the value of %1 is copied to %0, the value of %2 is copied to %1, and so on. This is useful for writing a batch file that performs the same operation on any number of parameters.
So the command shift always discards the previous value of $1, and shift 2 always discards the previous values of $1 and $2.
$@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.
var params = Array.prototype.slice.call(arguments); params.shift();
You can check out this blog post which explains it in further detail.
I assume you want to reference the original arguments
, instead of that from the callback you're passing to chrome.tabs.executeScript
.
If so, you'll need to cache it first.
function runScripts() { if (arguments.length === 0) return; var args = []; Array.prototype.push.apply( args, arguments ); chrome.tabs.executeScript(null, { file: args.shift(); }, function() { // using the modified Array based on the original arguments object runScripts.apply(null, args); }); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With