Browse Source

fixed compatibility issues with SRWAre Iron

Gildas 7 years ago
parent
commit
1cbe4fef04
2 changed files with 52 additions and 6 deletions
  1. 50 4
      lib/browser-polyfill/custom-browser-polyfill.js
  2. 2 2
      lib/single-file/htmlmini.js

+ 50 - 4
lib/browser-polyfill/custom-browser-polyfill.js

@@ -24,6 +24,8 @@
 
 	const isChrome = navigator.userAgent.includes("Chrome");
 
+	const FEATURE_TESTS = {};
+
 	if (isChrome && !this.browser) {
 		this.browser = {
 			browserAction: {
@@ -36,28 +38,72 @@
 					if (chrome.runtime.lastError) {
 						reject(chrome.runtime.lastError);
 					} else {
-						chrome.browserAction.setBadgeText(options, resolve);
+						try {
+							if (!FEATURE_TESTS["browserAction.setBadgeText"] || !FEATURE_TESTS["browserAction.setBadgeText"].callbackNotSupported) {
+								chrome.browserAction.setBadgeText(options, resolve);
+							} else {
+								chrome.browserAction.setBadgeText(options);
+								resolve();
+							}
+						} catch (error) {
+							FEATURE_TESTS["browserAction.setBadgeText"] = { callbackNotSupported: false };
+							chrome.browserAction.setBadgeText(options);
+							resolve();
+						}
 					}
 				}),
 				setBadgeBackgroundColor: options => new Promise((resolve, reject) => {
 					if (chrome.runtime.lastError) {
 						reject(chrome.runtime.lastError);
 					} else {
-						chrome.browserAction.setBadgeBackgroundColor(options, resolve);
+						try {
+							if (!FEATURE_TESTS["browserAction.setBadgeBackgroundColor"] || !FEATURE_TESTS["browserAction.setBadgeBackgroundColor"].callbackNotSupported) {
+								chrome.browserAction.setBadgeBackgroundColor(options, resolve);
+							} else {
+								chrome.browserAction.setBadgeBackgroundColor(options);
+								resolve();
+							}
+						} catch (error) {
+							FEATURE_TESTS["browserAction.setBadgeBackgroundColor"] = { callbackNotSupported: false };
+							chrome.browserAction.setBadgeBackgroundColor(options);
+							resolve();
+						}
 					}
 				}),
 				setTitle: options => new Promise((resolve, reject) => {
 					if (chrome.runtime.lastError) {
 						reject(chrome.runtime.lastError);
 					} else {
-						chrome.browserAction.setTitle(options, resolve);
+						try {
+							if (!FEATURE_TESTS["browserAction.setTitle"] || !FEATURE_TESTS["browserAction.setTitle"].callbackNotSupported) {
+								chrome.browserAction.setTitle(options, resolve);
+							} else {
+								chrome.browserAction.setTitle(options);
+								resolve();
+							}
+						} catch (error) {
+							FEATURE_TESTS["browserAction.setTitle"] = { callbackNotSupported: false };
+							chrome.browserAction.setTitle(options);
+							resolve();
+						}
 					}
 				}),
 				setIcon: options => new Promise((resolve, reject) => {
 					if (chrome.runtime.lastError) {
 						reject(chrome.runtime.lastError);
 					} else {
-						chrome.browserAction.setIcon(options, resolve);
+						try {
+							if (!FEATURE_TESTS["browserAction.setIcon"] || !FEATURE_TESTS["browserAction.setIcon"].callbackNotSupported) {
+								chrome.browserAction.setIcon(options, resolve);
+							} else {
+								chrome.browserAction.setIcon(options);
+								resolve();
+							}
+						} catch (error) {
+							FEATURE_TESTS["browserAction.setIcon"] = { callbackNotSupported: false };
+							chrome.browserAction.setIcon(options);
+							resolve();
+						}
 					}
 				})
 			},

+ 2 - 2
lib/single-file/htmlmini.js

@@ -143,7 +143,7 @@ this.htmlmini = this.htmlmini || (() => {
 
 	function collapseBooleanAttributes(node) {
 		if (node.nodeType == Node.ELEMENT_NODE) {
-			node.getAttributeNames().forEach(attributeName => {
+			Array.from(node.attributes).forEach(attributeName => {
 				if (booleanAttributes.includes(attributeName)) {
 					node.setAttribute(attributeName, "");
 				}
@@ -193,7 +193,7 @@ this.htmlmini = this.htmlmini || (() => {
 
 	function removeEmptyAttributes(node) {
 		if (node.nodeType == Node.ELEMENT_NODE) {
-			node.getAttributeNames().forEach(attributeName => {
+			Array.from(node.attributes).forEach(attributeName => {
 				if (safeToRemoveAttrs.includes(attributeName.toLowerCase())) {
 					const attributeValue = node.getAttribute(attributeName);
 					if (attributeValue === "" || (attributeValue || "").match(/^\s+$/)) {