/*
	MTDBackground.js
	Copyright (c) 2014-2020 dangered wolf, et al

	Released under the MIT licence
*/

var browser = browser || chrome;

if (browser !== "undefined") {

	browser.webRequest.onHeadersReceived.addListener((details) => {
		if (details.type !== "main_frame" && details.type !== "sub_frame") {
			return;
		}

		for (i = 0; i < details.responseHeaders.length; i++) {
			if (typeof details.responseHeaders[i].name !== "undefined" && details.responseHeaders[i].name === "content-security-policy") {
				details.responseHeaders[i].value = "default-src 'self'; connect-src * https://api.moderndeck.org; font-src https: data: blob: *; frame-src https:; frame-ancestors 'self' https:; img-src https: data: blob:; media-src *; object-src 'self' https: data: blob:; script-src 'self' 'unsafe-eval' 'unsafe-inline' https://c6.patreon.com https://moderndeck.org https://sentry.io https://cdn.jsdelivr.net https://ajax.googleapis.com https://cdn.ravenjs.com/ https://*.twitter.com https://*.twimg.com https://api-ssl.bitly.com; style-src 'self' 'unsafe-inline' https:;";
				return {responseHeaders:details.responseHeaders};
			}
		}

	}, {urls:["https://tweetdeck.twitter.com/*","https://twitter.com/i/cards/*"]});

	browser.webRequest.onBeforeRequest.addListener((details) => {

		if (details.url.indexOf(".css") > -1 && (details.url.indexOf("tfw/css") > -1 && details.url.indexOf("tweetdeck_bundle") > -1)) {
			return ({redirectURL:browser.runtime.getURL("sources/cssextensions/twittercard.css")});
		}

		return;
	}, {urls:["https://ton.twimg.com/*"]});

	browser.runtime.onMessage.addListener((m) => {
		console.log("Message received");
		console.log(m);
		if (m == "getStorage") {
			browser.storage.local.get(null, (items) => {
				browser.tabs.query({url: "https://tweetdeck.twitter.com/"}, (tabs) => {
					if (typeof tabs[0] !== "undefined") {
						browser.tabs.sendMessage(tabs[0].id, {"name": "sendStorage", "storage": JSON.stringify(items)});
					} else {
						browser.tabs.sendMessage(tabs.id, {"name": "sendStorage", "message": JSON.stringify(items)});
					}
					console.log("Reply sent");
					console.log(items);
				});
			});
		} else if (m.name == "setStorage") {
			browser.storage.local.set(m.content);
		}
	});
}

@@ -1213,1031 +1213,7 @@
 %5D%7D);
-%0A%0A%09browser.webRequest.onBeforeRequest.addListener((details) =%3E %7B%0A%0A%09%09if (details.url.indexOf(%22.css%22) %3E -1 && (details.url.indexOf(%22tfw/css%22) %3E -1 && details.url.indexOf(%22tweetdeck_bundle%22) %3E -1)) %7B%0A%09%09%09return (%7BredirectURL:browser.runtime.getURL(%22sources/cssextensions/twittercard.css%22)%7D);%0A%09%09%7D%0A%0A%09%09return;%0A%09%7D, %7Burls:%5B%22https://ton.twimg.com/*%22%5D%7D);%0A%0A%09browser.runtime.onMessage.addListener((m) =%3E %7B%0A%09%09console.log(%22Message received%22);%0A%09%09console.log(m);%0A%09%09if (m == %22getStorage%22) %7B%0A%09%09%09browser.storage.local.get(null, (items) =%3E %7B%0A%09%09%09%09browser.tabs.query(%7Burl: %22https://tweetdeck.twitter.com/%22%7D, (tabs) =%3E %7B%0A%09%09%09%09%09if (typeof tabs%5B0%5D !== %22undefined%22) %7B%0A%09%09%09%09%09%09browser.tabs.sendMessage(tabs%5B0%5D.id, %7B%22name%22: %22sendStorage%22, %22storage%22: JSON.stringify(items)%7D);%0A%09%09%09%09%09%7D else %7B%0A%09%09%09%09%09%09browser.tabs.sendMessage(tabs.id, %7B%22name%22: %22sendStorage%22, %22message%22: JSON.stringify(items)%7D);%0A%09%09%09%09%09%7D%0A%09%09%09%09%09console.log(%22Reply sent%22);%0A%09%09%09%09%09console.log(items);%0A%09%09%09%09%7D);%0A%09%09%09%7D);%0A%09%09%7D else if (m.name == %22setStorage%22) %7B%0A%09%09%09browser.storage.local.set(m.content);%0A%09%09%7D%0A%09%7D);
 %0A%7D%0A

Update MTDBackground.js