<commit_before>// Bootstrap the extension
let { Cc, Ci, Cu } = require('chrome');
let { gDevTools } = Cu.import("resource:///modules/devtools/gDevTools.jsm");

const { Panel } = require("dev/panel");
const { Disposable, setup } = require("sdk/core/disposable");
const { Class } = require("sdk/core/heritage");
const pageMod = require("sdk/page-mod");
const self = require("sdk/self");
const { MessageChannel } = require("sdk/messaging");
const tabs = require("sdk/tabs");
const { each, pairs, values } = require("sdk/util/sequence");

const workers = []; // for communicating with the Web pages under test

function detachWorker(worker, workerArray) {
	for (var index = 0; index < workerArray.length; index++) {
		if (workerArray[index].worker === worker) {
			break;
		}
	}
	if (index < workerArray.length) {
		// TODO: memory leak?
		workerArray[index].worker = undefined;
		workerArray[index].addonSide = undefined;
		workerArray[index].panelSide = undefined;

	}
}

function matchWorker(worker, workerArray) {
	for (var index = 0; index < workerArray.length; index++) {
		if (workerArray[index].worker === worker) {
			break;
		}
	}
	if (index < workerArray.length) {
		return workerArray[index];
	}
	return undefined;
}

function matchTabId(tabId, workerArray) {
	for (var index = 0; index < workerArray.length; index++) {
		if (workerArray[index].tabId === tabId) {
			break;
		}
	}
	if (index < workerArray.length) {
		return workerArray[index];
	}
	return undefined;

}

// Inject aXe into every frame
pageMod.PageMod({
	include: "*",
	contentScriptFile: self.data.url("axe.js"),
	attachTo: ['existing', 'frame']
});

// Inject aXe and the Web page communicator into the top frame
pageMod.PageMod({
	include: "*",
	contentScriptFile: [
		self.data.url("axe.js"),
		self.data.url("axe-communicator.js")
	],
	attachTo: ['existing', 'top'],
	onAttach: function(worker) {
		// Set up the channel for communicatig with our panel UI
		var channel = new MessageChannel();
		var workerEntry = matchTabId(worker.tab.id, workers);
		// messages from the panel arrive here
		channel.port1.onmessage = function(event) {
			let message = JSON.parse(event.data);
			switch (message.command) {
				case 'analyze':
					// console.log('analyze');
					worker.port.emit("axe.analyze");
					break;
				case 'highlight':
				case 'inspect':
					// TODO: frames
					let workerEntry = matchTabId(worker.tab.id, workers);
					let toolbox = workerEntry.panel.toolbox;
					let toolpanels = toolbox.getToolPanels();
					let inspector = toolpanels.get('inspector');
					inspector = toolpanels.get('inspector');
					let walker = inspector.walker;
					inspector.inspector.walker.querySelector(inspector.inspector.walker.rootNode, message.target[0])
						.then(function (result) {
							if (message.command === 'inspect') {
								toolbox.selection.setNodeFront(result);
								toolbox.selectTool('inspector');
							} else {
								toolbox.highlighterUtils.highlightNodeFront(result)
							}
							// console.log('inspector.inspector walker selector result: ' + result.nodeName);
						});
					break;
			}
		}
		if (!workerEntry) {
			workers.push({
				worker: worker,
				tabId: worker.tab.id,
				addonSide: channel.port1,
				panelSide: channel.port2
			});
			worker.tab.on('pageshow', function () {<commit_msg>add support for highlighting and inspecting in iframes<commit_after>// Bootstrap the extension
let { Cc, Ci, Cu } = require('chrome');
let { gDevTools } = Cu.import("resource:///modules/devtools/gDevTools.jsm");

const { Panel } = require("dev/panel");
const { Disposable, setup } = require("sdk/core/disposable");
const { Class } = require("sdk/core/heritage");
const pageMod = require("sdk/page-mod");
const self = require("sdk/self");
const { MessageChannel } = require("sdk/messaging");
const tabs = require("sdk/tabs");
const { each, pairs, values } = require("sdk/util/sequence");

const workers = []; // for communicating with the Web pages under test

function detachWorker(worker, workerArray) {
	for (var index = 0; index < workerArray.length; index++) {
		if (workerArray[index].worker === worker) {
			break;
		}
	}
	if (index < workerArray.length) {
		// TODO: memory leak?
		workerArray[index].worker = undefined;
		workerArray[index].addonSide = undefined;
		workerArray[index].panelSide = undefined;

	}
}

function matchWorker(worker, workerArray) {
	for (var index = 0; index < workerArray.length; index++) {
		if (workerArray[index].worker === worker) {
			break;
		}
	}
	if (index < workerArray.length) {
		return workerArray[index];
	}
	return undefined;
}

function matchTabId(tabId, workerArray) {
	for (var index = 0; index < workerArray.length; index++) {
		if (workerArray[index].tabId === tabId) {
			break;
		}
	}
	if (index < workerArray.length) {
		return workerArray[index];
	}
	return undefined;

}

// Inject aXe into every frame
pageMod.PageMod({
	include: "*",
	contentScriptFile: self.data.url("axe.js"),
	attachTo: ['existing', 'frame']
});

// Inject aXe and the Web page communicator into the top frame
pageMod.PageMod({
	include: "*",
	contentScriptFile: [
		self.data.url("axe.js"),
		self.data.url("axe-communicator.js")
	],
	attachTo: ['existing', 'top'],
	onAttach: function(worker) {
		// Set up the channel for communicatig with our panel UI
		var channel = new MessageChannel();
		var workerEntry = matchTabId(worker.tab.id, workers);
		// messages from the panel arrive here
		channel.port1.onmessage = function(event) {
			let message = JSON.parse(event.data);
			switch (message.command) {
				case 'analyze':
					// console.log('analyze');
					worker.port.emit("axe.analyze");
					break;
				case 'highlight':
				case 'inspect':
					// TODO: frames
					let workerEntry = matchTabId(worker.tab.id, workers);
					let toolbox = workerEntry.panel.toolbox;
					let toolpanels = toolbox.getToolPanels();
					let inspector = toolpanels.get('inspector');
					inspector = toolpanels.get('inspector');
					let walker = inspector.inspector.walker;
					walker.querySelector(walker.rootNode, message.target[0])
						.then(function (result) {
							var depth = 1, contentDocument;
							while(result.nodeName === 'IFRAME' && message.target.length > depth) {
								contentDocument = result.rawNode().contentDocument;
								result = walker.frontForRawNode(contentDocument.querySelector(message.target[depth]));
								depth += 1;
							}
							if (message.command === 'inspect') {
								toolbox.selection.setNodeFront(result);
								toolbox.selectTool('inspector');
							} else {
								toolbox.highlighterUtils.highlightNodeFront(result)
							}
							// console.log('inspector.inspector walker selector result: ' + result.nodeName);
						});
					break;
			}
		}
		if (!workerEntry) {
			workers.push({
				worker: worker,
				tabId: worker.tab.id,
				addonSide: channel.port1,
				panelSide: channel.port2
			});
			worker.tab.on('pageshow', function () {
{'fraction_non_alphanumeric': 0.1474063400576369, 'fraction_numerical': 0.0023054755043227667, 'mean_word_length': 3.661517797179315, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 4, 'https://': 0, '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': '593167', 'n_tokens_mistral': 2604, 'n_tokens_neox': 2181, 'n_words': 501}