Skip to content

Commit

Permalink
fix: better background-image url
Browse files Browse the repository at this point in the history
  • Loading branch information
painty committed Jan 15, 2023
1 parent da0f3c0 commit 1922320
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 39 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
74 changes: 38 additions & 36 deletions src/util/generateRulesAll.ts
Original file line number Diff line number Diff line change
@@ -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<ReturnType<typeof convTextToRules>>;
type cssNodeObj = Awaited<ReturnType<typeof convTextToRules>>

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

0 comments on commit 1922320

Please sign in to comment.