<commit_before>import Bluebird from 'bluebird'
import {parseHtml} from '../parse-html'
import forOwn from 'lodash/forOwn'
import forEach from 'lodash/forEach'
import {v4 as uuid} from 'uuid'

export class ExtensionNotLoadedError extends Error {}
export class ExtensionTooOldError extends Error {}

export function fetchHtml(url, fetchArgs) {
	if (!global.gobbldygook_extension) {
		return Bluebird.reject(new ExtensionNotLoadedError('Extension not loaded'))
	}
	if (global.gobbldygook_extension < '1.0.0') {
		return Bluebird.reject(new ExtensionTooOldError(`Extension version ${global.gobbldygook_extension_version} is too old.`))
	}

	// now we know the extension is loaded
	return new Bluebird((resolve, reject) => {
		const id = uuid()

		function handleResponse(event) {
			// `event` should have the shape {from, id, text}
			if (event.data.from !== 'web-ext') {
				return
			}
			if (event.data.id !== id) {
				return
			}

			global.removeEventListener('message', handleResponse)

			if (event.data.error) {
				reject(event.data.error)
			}
			else {
				resolve(html(event.data.text))
			}
		}

		global.addEventListener('message', handleResponse)

		global.postMessage({
			from: 'page',
			id,
			url,
			fetchArgs,
		}, '*')
	})
}


export function html(text) {
	return parseHtml(text)
}

export function buildFormData(obj) {
	let formData = new FormData()
	forOwn(obj, (val, key) => {
		formData.append(key, val)
	})
	return formData
}

export function getText(elems) {<commit_msg>remove extra function call from import-student<commit_after>import Bluebird from 'bluebird'
import {parseHtml} from '../parse-html'
import forOwn from 'lodash/forOwn'
import forEach from 'lodash/forEach'
import {v4 as uuid} from 'uuid'

export class ExtensionNotLoadedError extends Error {}
export class ExtensionTooOldError extends Error {}

export function fetchHtml(url, fetchArgs) {
	if (!global.gobbldygook_extension) {
		return Bluebird.reject(new ExtensionNotLoadedError('Extension not loaded'))
	}
	if (global.gobbldygook_extension < '1.0.0') {
		return Bluebird.reject(new ExtensionTooOldError(`Extension version ${global.gobbldygook_extension_version} is too old.`))
	}

	// now we know the extension is loaded
	return new Bluebird((resolve, reject) => {
		const id = uuid()

		function handleResponse(event) {
			// `event` should have the shape {from, id, text}
			if (event.data.from !== 'web-ext') {
				return
			}
			if (event.data.id !== id) {
				return
			}

			global.removeEventListener('message', handleResponse)

			if (event.data.error) {
				reject(event.data.error)
			}
			else {
				resolve(parseHtml(event.data.text))
			}
		}

		global.addEventListener('message', handleResponse)

		global.postMessage({
			from: 'page',
			id,
			url,
			fetchArgs,
		}, '*')
	})
}

export function buildFormData(obj) {
	let formData = new FormData()
	forOwn(obj, (val, key) => {
		formData.append(key, val)
	})
	return formData
}

export function getText(elems) {
{'fraction_non_alphanumeric': 0.12605042016806722, 'fraction_numerical': 0.002689075630252101, 'mean_word_length': 4.104631217838765, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '673553', 'n_tokens_mistral': 1071, 'n_tokens_neox': 957, 'n_words': 246}