Multi-parameter substition in Firefox bookmarks - commented javascript


Parent post: https://l14k.com/blog/firefox/


javascript:
var arguments = '%s'; 
/* This captures the expanded arguments provided, not literal %s */

originalURL = 'https://www.google.com/map/dir/%s/%s';
tempArgs = '';
quoteChar = 0;
chunks = originalURL.split('%s'); 
/* split the URL on the literal %s provided in orginalURL */


/* This chunk replaces spaces between quotes with carets so that the argument list can be correctly split up, using space as a delimiter. We don't want to split the argument list on a space within quotes*/
for (i = 0; i < arguments.length; i++){
    if (arguments.charAt(i) == '"');
        quoteChar = quoteChar^1; 
        /* XOR; toggles on/off at quote character - so as we iterate over a quoted argument, toggles on' for the opening quote and 'off' again for the closing quote */
    
    tempArgs += ((arguments.charAt(i) ==' ' && qc)?'^':arguments.charAt(i)); 
    /* Constructing adjusted arguments with shorthand conditional ("ternary operator"): (if?then:else). If we're inside quotes and encounter a space, replace with caret; otherwise don't replace the character. Only unquoted spaces will remain */
    
    }
args = tempArgs.split(/\s/); 
/* Split the adjusted args on space - since now only unquoted spaces remain. These become the new arguments for future operations */

newURL = '';
for (i = 0; i < chunks.length; i++){
    newURL += chunks[i];
    if (args[i] != undefined) {
        args[i] = args[i].replace(/\^/g,' '); 
        /* If an argument exists, replace the carets with space within that argument. This fixes the previous work done to correctly split the argument list*/
        
        newURL += args[i]; 
        /* Build the URL, alternating original chunks and argument chunks. URL was split on arg placeholders (%s), so we should always have chunk+arg+chunk(+arg...) */ 
        }
    }
location.replace(newURL); /* replaces the current browser location; ie, opens in the current tab */