From 43974b2d824deaad36fda7b808ff36a67f3e48e0 Mon Sep 17 00:00:00 2001
From: Pierre Ranwez <pierre.ranwez.etu@univ-lille.fr>
Date: Thu, 10 Feb 2022 19:01:48 +0100
Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20pure=20css?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 content.js |  22 ++++++--
 main.js    | 161 ++++++++++++++++++++++++++++++-----------------------
 2 files changed, 107 insertions(+), 76 deletions(-)

diff --git a/content.js b/content.js
index 62db428..66716a2 100644
--- a/content.js
+++ b/content.js
@@ -538,6 +538,21 @@ function audioEvent() {
     }
 }
 
+function pureCss() {
+    chrome.storage.sync.get(['all'], function (result) {
+        result.all.forEach(element => {
+            element.templates.forEach(template => {
+                if (template.type == 'pure') {
+                    let els = document.querySelectorAll(element.element);
+                    els.forEach(e => {
+                        e.style[element.property] = element.value;
+                    });
+                }
+            });
+        });
+    });
+}
+
 /**
  * Function trigger when new message received.
  * @param {type, data} message parameters 
@@ -546,16 +561,13 @@ function onMessage({ type, data }) {
     console.info('Message received from CSSLSD back :', type);
     switch (type) {
         case 'update': {
-            const css = data.cssStr;
-            audioEvents = [];
-            midiEvents = [];
-            // parseCSS(css)
+            pureCss();
             break;
         }
         case 'start': {
             chrome.storage.sync.get(['activate'], function (result) {
                 if (result.activate) {
-                    // zzz
+                    pureCss();
                 }
                 else {
                     audioApi();
diff --git a/main.js b/main.js
index 621a9b9..6f8f126 100644
--- a/main.js
+++ b/main.js
@@ -99,7 +99,7 @@ function initUi() {
 				tbl_row += "<td contenteditable>" + p[index] + "</td>";
 
 			}
-			tbl_body += "<tr class=\"" + (odd_even ? "odd" : "even") + "\"><td>" + icon + plus + minus+"</td>" + tbl_row + "</tr>";
+			tbl_body += "<tr class=\"" + (odd_even ? "odd" : "even") + "\"><td>" + icon + plus + minus + "</td>" + tbl_row + "</tr>";
 			odd_even = !odd_even;
 		});
 		$("#parameters").html('');
@@ -137,82 +137,96 @@ function analyseAll() {
 		var all = [];
 		var parser = new CSSParser();
 		var sheet = parser.parse(result.css, false, false);
-		console.log(sheet.cssRules);
 		sheet.cssRules.forEach(element => {
 			element.declarations.forEach(css => {
 				var templates = [];
 				cssValue = css.valueText;
-				for (let index = 0; index < css.valueText.split("[").length - 1; index++) {
-					template = cssValue.substring(
-						cssValue.indexOf("[") + 1,
-						cssValue.indexOf("]")
-					);
-					cssValue = cssValue.replace("[" + template + "]", "");
-					var parameters = JSON.parse(result.parameters);
-					parameters.forEach(p => {
-						templateSplited = template.split(' ');
-						if (p[1] == templateSplited[0]) {
-							if (p[4] != "") {
-								p[4].split(' ').forEach(midiE => {
-									var obj = {
-										type: '',
-										name: templateSplited[0],
-										templateFull: template,
-										eventType: '',
-										eventName: '',
-										options: '',
-										array: templateSplited.length > 1 ? (templateSplited[1].includes(';') ? templateSplited[1].split(';') : '') : '',
-										min: templateSplited.length > 1 ? (templateSplited[1].includes(':') ? templateSplited[1].split(':')[0] : '') : '',
-										max: templateSplited.length > 1 ? (templateSplited[1].includes(':') ? templateSplited[1].split(':')[1] : '') : '',
-									}
-									obj.type = 'midi';
-									if (midiE.includes(':')) {
-										obj.eventType = midiE.split(':')[0];
-										obj.eventName = midiE.split(':')[1];
-									}
-									else {
-										obj.eventType = midiE;
-										obj.eventName = '';
-									}
-									obj.options = p[3];
-									obj.array = ((obj.array == '') ? (p[2].includes(';') ? p[2].split(';') : '') : obj.array);
-									obj.min = ((obj.min == '') ? (p[2].includes(':') ? p[2].split(':')[0] : 0) : obj.min);
-									obj.max = ((obj.max == '') ? (p[2].includes(':') ? p[2].split(':')[1] : 1) : obj.max);
-									templates.push(obj);
-								});
+				count = css.valueText.split("[").length;
+				if (count > 1) {
+					for (let index = 0; index < count - 1; index++) {
+						template = cssValue.substring(
+							cssValue.indexOf("[") + 1,
+							cssValue.indexOf("]")
+						);
+						cssValue = cssValue.replace("[" + template + "]", "");
+						var parameters = JSON.parse(result.parameters);
+						parameters.forEach(p => {
+							templateSplited = template.split(' ');
+							if (p[1] == templateSplited[0]) {
+								if (p[4] != "") {
+									p[4].split(' ').forEach(midiE => {
+										var obj = {
+											type: '',
+											name: templateSplited[0],
+											templateFull: template,
+											eventType: '',
+											eventName: '',
+											options: '',
+											array: templateSplited.length > 1 ? (templateSplited[1].includes(';') ? templateSplited[1].split(';') : '') : '',
+											min: templateSplited.length > 1 ? (templateSplited[1].includes(':') ? templateSplited[1].split(':')[0] : '') : '',
+											max: templateSplited.length > 1 ? (templateSplited[1].includes(':') ? templateSplited[1].split(':')[1] : '') : '',
+										}
+										obj.type = 'midi';
+										if (midiE.includes(':')) {
+											obj.eventType = midiE.split(':')[0];
+											obj.eventName = midiE.split(':')[1];
+										}
+										else {
+											obj.eventType = midiE;
+											obj.eventName = '';
+										}
+										obj.options = p[3];
+										obj.array = ((obj.array == '') ? (p[2].includes(';') ? p[2].split(';') : '') : obj.array);
+										obj.min = ((obj.min == '') ? (p[2].includes(':') ? p[2].split(':')[0] : 0) : obj.min);
+										obj.max = ((obj.max == '') ? (p[2].includes(':') ? p[2].split(':')[1] : 1) : obj.max);
+										templates.push(obj);
+									});
+								}
+								if (p[5] != "") {
+									p[5].split(' ').forEach(audioE => {
+										var obj = {
+											type: '',
+											name: templateSplited[0],
+											templateFull: template,
+											eventType: '',
+											eventName: '',
+											options: '',
+											array: templateSplited.length > 1 ? (templateSplited[1].includes(';') ? templateSplited[1].split(';') : '') : '',
+											min: templateSplited.length > 1 ? (templateSplited[1].includes(':') ? templateSplited[1].split(':')[0] : '') : '',
+											max: templateSplited.length > 1 ? (templateSplited[1].includes(':') ? templateSplited[1].split(':')[1] : '') : '',
+										}
+										obj.type = 'audio';
+										if (audioE.includes(':')) {
+											obj.eventType = audioE.split(':')[0];
+											obj.eventName = audioE.split(':')[1];
+										}
+										else {
+											obj.eventType = audioE;
+											obj.eventName = '';
+										}
+										obj.options = p[3];
+										obj.array = ((obj.array == '') ? (p[2].includes(';') ? p[2].split(';') : '') : obj.array);
+										obj.min = ((obj.min == '') ? (p[2].includes(':') ? p[2].split(':')[0] : 0) : obj.min);
+										obj.max = ((obj.max == '') ? (p[2].includes(':') ? p[2].split(':')[1] : 1) : obj.max);
+										templates.push(obj);
+									});
+								}
 							}
-							if (p[5] != "") {
-								p[5].split(' ').forEach(audioE => {
-									var obj = {
-										type: '',
-										name: templateSplited[0],
-										templateFull: template,
-										eventType: '',
-										eventName: '',
-										options: '',
-										array: templateSplited.length > 1 ? (templateSplited[1].includes(';') ? templateSplited[1].split(';') : '') : '',
-										min: templateSplited.length > 1 ? (templateSplited[1].includes(':') ? templateSplited[1].split(':')[0] : '') : '',
-										max: templateSplited.length > 1 ? (templateSplited[1].includes(':') ? templateSplited[1].split(':')[1] : '') : '',
-									}
-									obj.type = 'audio';
-									if (audioE.includes(':')) {
-										obj.eventType = audioE.split(':')[0];
-										obj.eventName = audioE.split(':')[1];
-									}
-									else {
-										obj.eventType = audioE;
-										obj.eventName = '';
-									}
-									obj.options = p[3];
-									obj.array = ((obj.array == '') ? (p[2].includes(';') ? p[2].split(';') : '') : obj.array);
-									obj.min = ((obj.min == '') ? (p[2].includes(':') ? p[2].split(':')[0] : 0) : obj.min);
-									obj.max = ((obj.max == '') ? (p[2].includes(':') ? p[2].split(':')[1] : 1) : obj.max);
-									templates.push(obj);
-								});
-							}
-						}
-					});
+						});
 
+					}
+				} else {
+					templates.push({
+						type: 'pure',
+						name: '',
+						templateFull: '',
+						eventType: '',
+						eventName: '',
+						options: '',
+						array: '',
+						min: '',
+						max: '',
+					});
 				}
 				all.push({
 					element: element.mSelectorText,
@@ -235,6 +249,11 @@ function updateCss() {
 	let cssStr = editor.getValue();
 	chrome.storage.sync.set({ css: cssStr });
 	analyseAll();
+	chrome.tabs.query({ active: true, currentWindow: true },
+		function (tabs) {
+			chrome.tabs.sendMessage(tabs[0].id, { type: 'update', data: false });
+		}
+	);
 }
 
 $('#download').on('click', function () {
-- 
GitLab