diff --git a/package-lock.json b/package-lock.json index c1b704f..537eb8f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "css-used-chrome-ext", - "version": "2.5.0", + "version": "3.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "css-used-chrome-ext", - "version": "2.5.0", + "version": "3.0.0", "license": "MIT", "dependencies": { "postcss": "^8.4.21", diff --git a/package.json b/package.json index 7431cb5..1cf7b3e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "css-used-chrome-ext", - "version": "2.5.0", + "version": "3.0.0", "type": "module", "description": "A chrome extension to get all css rules used by the selected DOM and its descendants.", "scripts": { diff --git a/src/util/generateRulesAll.ts b/src/util/generateRulesAll.ts index 1edf5f0..5667936 100644 --- a/src/util/generateRulesAll.ts +++ b/src/util/generateRulesAll.ts @@ -1,82 +1,84 @@ -import traversalCSSRuleList from "./traversalCSSRuleList"; -import convTextToRules from "./convTextToRules"; -import {cssHelper} from "./cssHelper"; -import convUrlToAbs from "./convUrlToAbs"; +import traversalCSSRuleList from './traversalCSSRuleList' +import convTextToRules from './convTextToRules' +import { cssHelper } from './cssHelper' +import convUrlToAbs from './convUrlToAbs' -type cssNodeObj = Awaited>; +type cssNodeObj = Awaited> function generateRulesAll( doc: Document, - externalCssCache: { [index: cssNodeObj["href"]]: cssNodeObj } + externalCssCache: { [index: cssNodeObj['href']]: cssNodeObj } ) { - var x: number; + var x: number var objCss = { normRule: [], fontFace: [], keyFram: [], - }; + } - var promises = []; + var promises = [] return new Promise(function (resolve, reject) { // loop every styleSheets for (x = 0; x < doc.styleSheets.length; x++) { - const styleSheet = doc.styleSheets[x]; + const styleSheet = doc.styleSheets[x] promises.push( new Promise(function (res) { - var cssNodeArr : cssNodeObj; + var cssNodeArr: cssNodeObj if (styleSheet.href !== null) { // can be link tag - cssNodeArr = externalCssCache[styleSheet.href]; - cssNodeArr.media = doc.styleSheets[x].media; - traversalCSSRuleList(doc, externalCssCache, cssNodeArr).then(res); - } else if(styleSheet.ownerNode instanceof Element) { + cssNodeArr = externalCssCache[styleSheet.href] + cssNodeArr.media = doc.styleSheets[x].media + traversalCSSRuleList(doc, externalCssCache, cssNodeArr).then(res) + } else if (styleSheet.ownerNode instanceof Element) { // style tag - let html: string = styleSheet.ownerNode.innerHTML; - if (html === "") { + let html: string = styleSheet.ownerNode.innerHTML + if (html === '') { // style may be in style-tag's cssRules but not show in innerHTML for ( let index = 0; index < doc.styleSheets[x].cssRules.length; index++ ) { - const rule = doc.styleSheets[x].cssRules[index]; - html += rule.cssText; + const rule = doc.styleSheets[x].cssRules[index] + html += rule.cssText } } // convert urls in style tag to abs html = html.replace( /url\((['"]?)(.*?)\1\)/g, - function (_a, _p1, p2) { - return "url(\"" + convUrlToAbs(doc.location.href, p2) + "\")"; + function (_a, p1, p2) { + return ( + 'url(' + p1 + convUrlToAbs(doc.location.href, p2) + p1 + ')' + ) } - ); + ) // the next operation is asynchronous // store the current x value - let _x = x; - convTextToRules(html, doc.location.href).then(cssNodeObj=>{ - cssNodeObj.media = doc.styleSheets[_x].media; - traversalCSSRuleList(doc, externalCssCache, cssNodeObj).then(res); - }); - }else{ + let _x = x + convTextToRules(html, doc.location.href).then((cssNodeObj) => { + cssNodeObj.media = doc.styleSheets[_x].media + traversalCSSRuleList(doc, externalCssCache, cssNodeObj).then(res) + }) + } else { // console.log('ProcessingInstruction', styleSheet.ownerNode); res({}) } }) - ); + ) } Promise.all(promises) .then(function (result) { result.forEach(function (ele) { - cssHelper.mergeobjCss(objCss, ele); - }); - resolve(objCss); + cssHelper.mergeobjCss(objCss, ele) + }) + resolve(objCss) }) .catch(function (err) { - reject(err); - }); - }); + reject(err) + }) + }) } -export default generateRulesAll; +export default generateRulesAll