'use strict';
/**
 * Handles matching page URL with defined connectors and injecting scripts into content document
 */
define((require) => {
	const browser = require('webextension-polyfill');
	const Options = require('storage/options');
	const UrlMatch = require('util/url-match');
	const connectors = require('connectors');
	const InjectResult = require('object/inject-result');
	const CustomPatterns = require('storage/custom-patterns');

	/**
	 * Ping the loaded page and checks if there is already loaded connector.
	 * If no connector is loaded, inject content scripts.
	 *
	 * @param {Number} tabId Tab ID
	 * @param {Object} connector Connector match object
	 *
	 * @return {Object} InjectResult value
	 */
	async function pingAndInject(tabId, connector) {
		// Ping the content page to see if the script is already in place.
		try {
			await browser.tabs.sendMessage(tabId, { type: 'EVENT_PING' });
		} catch (e) {
			return injectScripts(tabId, connector);
		}

		console.log(
			'Subsequent ajax navigation, the scripts are already injected');

		return new InjectResult(InjectResult.ALREADY_INJECTED, connector);
	}

	/**
	 * Inject content scripts into the page.
	 *
	 * @param {Number} tabId Tab ID
	 * @param {Object} connector Connector match object
	 *
	 * @return {Object} InjectResult value
	 */
	async function injectScripts(tabId, connector) {
		const scripts = connector.js.slice(0);
		scripts.unshift('core/content/connector.js');
		scripts.unshift('core/content/filter.js');
		scripts.unshift('core/content/reactor.js');
		// @ifdef DEBUG
		scripts.unshift('core/content/reporter.js');
		// @endif
		scripts.unshift('core/content/util.js');
		scripts.unshift('vendor/browser-polyfill.min.js');
		scripts.unshift('vendor/jquery.min.js');
		// Needs to be the last script injected
		scripts.push('core/content/starter.js');

		for (const script of scripts) {
			const injectDetails = {
				file: script,
				allFrames: connector.allFrames || false
			};

			console.log(`Injecting ${script}`);
			try {
				await browser.tabs.executeScript(tabId, injectDetails);
			} catch (e) {
				// Firefox throws an error if a content script returns no value.
				console.error(e);
			}
		}

		const isEnabled = await Options.isConnectorEnabled(connector.label);
		if (isEnabled) {
			return new InjectResult(
				InjectResult.MATCHED_AND_INJECTED, connector);
		}

		return new InjectResult(InjectResult.MATCHED_BUT_DISABLED, connector);
	}

	/**
	 * Is triggered by browser.tabs.onUpdated event
	 * Check for available connectors and injects matching connector into
	 * loaded page while returning info about the connector.
	 *
	 * @param  {Object} tab Tab object
	 * @return {Object} InjectResult value
	 */
	async function onTabsUpdated(tab) {
		// Asynchronously preload all custom patterns and then start matching
		let customPatterns = await CustomPatterns.getAllPatterns();
		for (let connector of connectors) {
			let matchOk = false;
			let patterns = connector.matches || [];

			if (customPatterns[connector.label]) {
				patterns = patterns.concat(customPatterns[connector.label]);
			}

			for (let pattern of patterns) {
				matchOk = matchOk || UrlMatch.test(tab.url, pattern);
			}

			if (matchOk) {
				/*
				 * Checks if there's already injected connector
				 * and injects it if needed.
				 */
				return pingAndInject(tab.id, connector);

			}
		}

		return new InjectResult(InjectResult.NO_MATCH, null);
	}

	return { onTabsUpdated };
});

@@ -434,16 +434,335 @@
 rns');%0A%0A
+%09const CONTENT_SCRIPTS = %5B%0A%09%09'vendor/jquery.min.js',%0A%09%09'vendor/browser-polyfill.min.js',%0A%09%09'core/content/util.js',%0A%09%09// @ifdef DEBUG%0A%09%09'core/content/reporter.js',%0A%09%09// @endif%0A%09%09'core/content/reactor.js',%0A%09%09'core/content/filter.js',%0A%09%09'core/content/connector.js',%0A%09%5D;%0A%09const STARTER_SCRIPT = 'core/content/starter.js';%0A%0A
 %09/**%0A%09 *
@@ -1698,468 +1698,91 @@
 s = 
-connector.js.slice(0);%0A%09%09scripts.unshift('core/content/connector.js');%0A%09%09scripts.unshift('core/content/filter.js');%0A%09%09scripts.unshift('core/content/reactor.js');%0A%09%09// @ifdef DEBUG%0A%09%09scripts.unshift('core/content/reporter.js');%0A%09%09// @endif%0A%09%09scripts.unshift('core/content/util.js');%0A%09%09scripts.unshift('vendor/browser-polyfill.min.js');%0A%09%09scripts.unshift('vendor/jquery.min.js');%0A%09%09// Needs to be the last script injected%0A%09%09scripts.push('core/content/starter.js'
+%5B%0A%09%09%09...CONTENT_SCRIPTS, ...connector.js, STARTER_SCRIPT%0A%09%09%5D;%0A%09%09console.log(scripts
 );%0A%0A

Use spread syntax to construct array of content scripts