<commit_before>/**
 * Chrome driver
 */

(function() {
	if ( wappalyzer == null ) {
		return;
	}

	var w = wappalyzer,
		firstRun = false,
		upgraded = false,
		tab,
		tabCache = {},
		headersCache = {};

	w.driver = {
		timeout: 1000,

		/**
		 * Log messages to console
		 */
		log: function(args) {
			console.log('[wappalyzer ' + args.type + '] ' + args.message);
		},

		/**
		 * Initialize
		 */
		init: function() {
			w.log('init');

			// Load apps.json
			var xhr = new XMLHttpRequest();

			xhr.open('GET', 'apps.json', true);

			xhr.overrideMimeType('application/json');

			xhr.onload = function() {
				var json = JSON.parse(xhr.responseText);

				w.categories = json.categories;
				w.apps       = json.apps;

				w.driver.categoryOrder = Object.keys(w.categories).sort(function(a, b) {
					return w.categories[a].priority - w.categories[b].priority;
				});

				console.log(w.driver.categoryOrder);
			};

			xhr.send(null);

			// Version check
			try {
				var version = browser.runtime.getManifest().version;

				if ( localStorage['version'] == null ) {
					firstRun = true;

					// Set defaults
					for ( var option in defaults ) {
						localStorage[option] = defaults[option];
					}
				} else if ( version !== localStorage['version'] && parseInt(localStorage['upgradeMessage'], 10) ) {
					upgraded = true;
				}

				localStorage['version'] = version;
			} catch(e) { }

      if ( typeof chrome === 'undefined' ) {
        browser.runtime.onMessage.addListener(w.driver.onMessage);
      } else {
        chrome.runtime.onMessage.addListener(w.driver.onMessage);
      }

			browser.tabs.query({}).then(function(tabs) {
				tabs.forEach(function(tab) {
					if ( tab.url.match(/^https?:\/\//) ) {
						browser.tabs.executeScript(tab.id, { file: 'js/content.js' });
					}
				})
			});

			browser.tabs.onRemoved.addListener(function(tabId) {
				w.log('remove tab');

				tabCache[tabId] = null;
			});

			// Live intercept headers using webRequest API
			browser.webRequest.onCompleted.addListener(function(details) {
				var responseHeaders = {};

				if ( details.responseHeaders ) {
					var uri = details.url.replace(/#.*$/, ''); // Remove hash

					details.responseHeaders.forEach(function(header) {
						responseHeaders[header.name.toLowerCase()] = header.value || '' + header.binaryValue;
					});

					if ( headersCache.length > 50 ) {
						headersCache = {};
					}

					if ( /text\/html/.test(responseHeaders['content-type']) ) {
						if ( headersCache[uri] === undefined ) {
							headersCache[uri] = {};
						}

						for ( var header in responseHeaders ) {
							headersCache[uri][header] = responseHeaders[header];
						}
					}

					w.log(JSON.stringify({ uri: uri, headers: responseHeaders }));
				}
			}, { urls: [ 'http://*/*', 'https://*/*' ], types: [ 'main_frame' ] }, [ 'responseHeaders' ]);

			if ( firstRun ) {
				w.driver.goToURL({ url: w.config.websiteURL + 'installed', medium: 'install' });

				firstRun = false;
			}

			if ( upgraded ) {
				w.driver.goToURL({ url: w.config.websiteURL + 'upgraded', medium: 'upgrade', background: true });

				upgraded = false;
			}
		},

		onMessage: function(message, sender, sendResponse) {
			var
				hostname,
				a = document.createElement('a');

			if ( typeof message.id != 'undefined' ) {
				w.log('message: ' + message.id);

				switch ( message.id ) {
					case 'log':
						w.log(message.message);

						break;
					case 'analyze':
						tab = sender.tab;

						a.href = tab.url.replace(/#.*$/, '');

						hostname = a.hostname;

						if ( headersCache[a.href] !== undefined ) {
							message.subject.headers = headersCache[a.href];
						}

						w.analyze(hostname, a.href, message.subject);

						break;
					case 'ad_log':
						w.adCache.push(message.subject);

						break;
					case 'get_apps':
						sendResponse({
							tabCache:   tabCache[message.tab.id],
							apps:       w.apps,
							categories: w.categories
							});

						break;
				}
			}
		},

		goToURL: function(args) {
			var url = args.url + ( typeof args.medium === 'undefined' ? '' :  '?pk_campaign=chrome&pk_kwd=' + args.medium);

			browser.tabs.create({ url: url, active: args.background === undefined || !args.background });
		},

		/**
		 * Display apps
		 */
		displayApps: function() {
			var
				url   = tab.url.replace(/#.*$/, ''),
				count = w.detected[url] ? Object.keys(w.detected[url]).length.toString() : '0';

			if ( tabCache[tab.id] == null ) {
				tabCache[tab.id] = {
					count: 0,
					appsDetected: []
					};
			}

			tabCache[tab.id].count        = count;
			tabCache[tab.id].appsDetected = w.detected[url];

			if ( count > 0 ) {
				// Find the main application to display<commit_msg>Chrome driver always sends response to foreground page to avoid foreground exception.<commit_after>/**
 * Chrome driver
 */

(function() {
	if ( wappalyzer == null ) {
		return;
	}

	var w = wappalyzer,
		firstRun = false,
		upgraded = false,
		tab,
		tabCache = {},
		headersCache = {};

	w.driver = {
		timeout: 1000,

		/**
		 * Log messages to console
		 */
		log: function(args) {
			console.log('[wappalyzer ' + args.type + '] ' + args.message);
		},

		/**
		 * Initialize
		 */
		init: function() {
			w.log('init');

			// Load apps.json
			var xhr = new XMLHttpRequest();

			xhr.open('GET', 'apps.json', true);

			xhr.overrideMimeType('application/json');

			xhr.onload = function() {
				var json = JSON.parse(xhr.responseText);

				w.categories = json.categories;
				w.apps       = json.apps;

				w.driver.categoryOrder = Object.keys(w.categories).sort(function(a, b) {
					return w.categories[a].priority - w.categories[b].priority;
				});

				console.log(w.driver.categoryOrder);
			};

			xhr.send(null);

			// Version check
			try {
				var version = browser.runtime.getManifest().version;

				if ( localStorage['version'] == null ) {
					firstRun = true;

					// Set defaults
					for ( var option in defaults ) {
						localStorage[option] = defaults[option];
					}
				} else if ( version !== localStorage['version'] && parseInt(localStorage['upgradeMessage'], 10) ) {
					upgraded = true;
				}

				localStorage['version'] = version;
			} catch(e) { }

      if ( typeof chrome === 'undefined' ) {
        browser.runtime.onMessage.addListener(w.driver.onMessage);
      } else {
        chrome.runtime.onMessage.addListener(w.driver.onMessage);
      }

			browser.tabs.query({}).then(function(tabs) {
				tabs.forEach(function(tab) {
					if ( tab.url.match(/^https?:\/\//) ) {
						browser.tabs.executeScript(tab.id, { file: 'js/content.js' });
					}
				})
			});

			browser.tabs.onRemoved.addListener(function(tabId) {
				w.log('remove tab');

				tabCache[tabId] = null;
			});

			// Live intercept headers using webRequest API
			browser.webRequest.onCompleted.addListener(function(details) {
				var responseHeaders = {};

				if ( details.responseHeaders ) {
					var uri = details.url.replace(/#.*$/, ''); // Remove hash

					details.responseHeaders.forEach(function(header) {
						responseHeaders[header.name.toLowerCase()] = header.value || '' + header.binaryValue;
					});

					if ( headersCache.length > 50 ) {
						headersCache = {};
					}

					if ( /text\/html/.test(responseHeaders['content-type']) ) {
						if ( headersCache[uri] === undefined ) {
							headersCache[uri] = {};
						}

						for ( var header in responseHeaders ) {
							headersCache[uri][header] = responseHeaders[header];
						}
					}

					w.log(JSON.stringify({ uri: uri, headers: responseHeaders }));
				}
			}, { urls: [ 'http://*/*', 'https://*/*' ], types: [ 'main_frame' ] }, [ 'responseHeaders' ]);

			if ( firstRun ) {
				w.driver.goToURL({ url: w.config.websiteURL + 'installed', medium: 'install' });

				firstRun = false;
			}

			if ( upgraded ) {
				w.driver.goToURL({ url: w.config.websiteURL + 'upgraded', medium: 'upgrade', background: true });

				upgraded = false;
			}
		},

		onMessage: function(message, sender, sendResponse) {
			var
				hostname,
				a = document.createElement('a'),
				response = undefined;

			if ( typeof message.id != 'undefined' ) {
				w.log('message: ' + message.id);

				switch ( message.id ) {
					case 'log':
						w.log(message.message);

						break;
					case 'analyze':
						tab = sender.tab;

						a.href = tab.url.replace(/#.*$/, '');

						hostname = a.hostname;

						if ( headersCache[a.href] !== undefined ) {
							message.subject.headers = headersCache[a.href];
						}

						w.analyze(hostname, a.href, message.subject);

						break;
					case 'ad_log':
						w.adCache.push(message.subject);

						break;
					case 'get_apps':
						response = {
							tabCache:   tabCache[message.tab.id],
							apps:       w.apps,
							categories: w.categories
						};

						break;
				}

				sendResponse( response );
			}

		},

		goToURL: function(args) {
			var url = args.url + ( typeof args.medium === 'undefined' ? '' :  '?pk_campaign=chrome&pk_kwd=' + args.medium);

			browser.tabs.create({ url: url, active: args.background === undefined || !args.background });
		},

		/**
		 * Display apps
		 */
		displayApps: function() {
			var
				url   = tab.url.replace(/#.*$/, ''),
				count = w.detected[url] ? Object.keys(w.detected[url]).length.toString() : '0';

			if ( tabCache[tab.id] == null ) {
				tabCache[tab.id] = {
					count: 0,
					appsDetected: []
					};
			}

			tabCache[tab.id].count        = count;
			tabCache[tab.id].appsDetected = w.detected[url];

			if ( count > 0 ) {
				// Find the main application to display
{'fraction_non_alphanumeric': 0.16847485916962238, 'fraction_numerical': 0.0022950135614437724, 'mean_word_length': 2.4912600145666426, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '7043086', 'n_tokens_mistral': 4065, 'n_tokens_neox': 3085, 'n_words': 574}