export async function getHostname() {
	const {rootUrl} = await syncStore.getAll();

	if (/(^(https:\/\/)?(api\.)?github\.com)/.test(rootUrl)) {
		return 'github.com';
	}

	return (new URL(rootUrl)).hostname;
}
export async function getApiUrl() {
	const {rootUrl} = await syncStore.getAll();

	if (/(^(https:\/\/)?(api\.)?github\.com)/.test(rootUrl)) {
		return 'https://api.github.com';
	}

	return `${rootUrl}api/v3`;
}
async function handleLastModified(newLastModified) {
	const lastModified = await localStore.get('lastModified') || new Date(0);

	// Something has changed since we last accessed, display any new notificaitons
	if (newLastModified !== lastModified) {
		const {showDesktopNotif, playNotifSound} = await syncStore.getAll();
		if (showDesktopNotif === true || playNotifSound === true) {
			await checkNotifications(lastModified);
		}

		await localStore.set('lastModified', newLastModified);
	}
}
export async function getHeaders() {
	const {token} = await syncStore.getAll();

	if (!(/[a-z\d]{40}/.test(token))) {
		throw new Error('missing token');
	}

	return {
		/* eslint-disable quote-props */
		'Authorization': `token ${token}`,
		'If-Modified-Since': ''
		/* eslint-enable quote-props */
	};
}
export async function getNotificationResponse({maxItems = 100, lastModified = ''} = {}) {
	const {onlyParticipating} = await syncStore.getAll();
	const params = {
		per_page: maxItems // eslint-disable-line camelcase
	};

	if (onlyParticipating) {
		params.participating = onlyParticipating;
	}

	if (lastModified) {
		params.since = lastModified;
	}

	return makeApiRequest('/notifications', params);
}
export async function getTabUrl() {
	const {onlyParticipating} = await syncStore.getAll();
	const useParticipating = onlyParticipating ? '/participating' : '';

	return `https://${await getHostname()}/notifications${useParticipating}`;
}
import {featuresDefaultValues} from './features';

const optionsSync = new OptionsSync();

// Define defaults
optionsSync.define({
	defaults: Object.assign({}, featuresDefaultValues, {
		logging: false
	}),
	migrations: [
		OptionsSync.migrations.removeUnused
	]
});

// Make sure that all features have an option value
optionsSync.getAll().then(options => {
	const newOptions = Object.assign({}, featuresDefaultValues, options);
	optionsSync.setAll(newOptions);
});

// Fix the extension when right-click saving a tweet image
browser.downloads.onDeterminingFilename.addListener((item, suggest) => {
	suggest({
		filename: item.filename.replace(/\.(jpg|png)_(large|orig)$/, '.$1')
	});
});

browser.webRequest.onBeforeRequest.addListener(({url}) => {
	if (url.endsWith(':large')) {
		return {
			redirectUrl: url.replace(/:large$/, ':orig')