<commit_before>//Version 0.2
//Declare rules:
var match_rules = {
    conditions: [
	   new chrome.declarativeContent.PageStateMatcher({
	       //find pages like 'https://*.scoutbook.com/*/reports/report.asp'
	       pageUrl: { 
		       hostSuffix: '.scoutbook.com', 
			   pathSuffix: '/reports/report.asp', 
			   schemes: ['https']
			}
	   })
	],
	//If found, display the Page Action icon registered in the manifest.json
	actions: [ new chrome.declarativeContent.ShowPageAction() ]
};

//Register rules:
chrome.runtime.onInstalled.addListener(function(details) {
    chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
        chrome.declarativeContent.onPageChanged.addRules([match_rules]);
    });
});

// Called when the user clicks on the browser action.
chrome.pageAction.onClicked.addListener(function(tab) {
  console.log('Clicked!');
      chrome.tabs.executeScript(null, {file: 'content_script.js'}, 	function(result){
	  chrome.tabs.executeScript(null, {file: 'bower_components/excellentexport/excellentexport.min.js'} , function() { 
		chrome.tabs.sendMessage(tab.id, {action: 'go'}, 
			function(response){
			    console.log('v0.2'+response); 
				//Reload the tab to clean up residual scripts still hanging around
                //chrome.tabs.reload ();

		    });
	    });
    });
 
});
<commit_msg>Refactored code for more elegant content script loading.<commit_after>//Version 0.2
var version = '0.2';
//Declare rules:
var match_rules = {
    conditions: [
	   new chrome.declarativeContent.PageStateMatcher({
	       //find pages like 'https://*.scoutbook.com/*/reports/report.asp'
	       pageUrl: { 
		       hostSuffix: '.scoutbook.com', 
			   pathSuffix: '/reports/report.asp', 
			   schemes: ['https']
			}
	   })
	],
	//If found, display the Page Action icon registered in the manifest.json
	actions: [ new chrome.declarativeContent.ShowPageAction() ]
};

//Register rules:
chrome.runtime.onInstalled.addListener(function(details) {
    chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
        chrome.declarativeContent.onPageChanged.addRules([match_rules]);
    });
});

// Called when the user clicks on the browser action.
chrome.pageAction.onClicked.addListener(function(tab) {
  console.log('Clicked!');
  chrome.tabs.insertCSS(null, {file: "bower_components/sweetalert/lib/sweet-alert.css"}, 
    executeScripts(null, [ 
        { file: "bower_components/excellentexport/excellentexport.min.js" }, 
        { file: "bower_components/sweetalert/lib/sweet-alert.min.js" },
        { file: "content_script.js" },
        { code: "exportTable();" }
    ])
  ) 
});

//From http://stackoverflow.com/questions/21535233/injecting-multiple-scripts-through-executescript-in-google-chrome
function executeScripts(tabId, injectDetailsArray)
{
    function createCallback(tabId, injectDetails, innerCallback) {
        return function () {
            chrome.tabs.executeScript(tabId, injectDetails, innerCallback);
        };
    }

    var callback = function(response){ console.log(version+' '+response); };

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

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

{'fraction_non_alphanumeric': 0.13935912938331319, 'fraction_numerical': 0.005441354292623942, 'mean_word_length': 3.435656836461126, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2676872', 'n_tokens_mistral': 1080, 'n_tokens_neox': 991, 'n_words': 187}