Skip to content

Commit 1922320

Browse files
committed
fix: better background-image url
1 parent da0f3c0 commit 1922320

File tree

3 files changed

+41
-39
lines changed

3 files changed

+41
-39
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "css-used-chrome-ext",
3-
"version": "2.5.0",
3+
"version": "3.0.0",
44
"type": "module",
55
"description": "A chrome extension to get all css rules used by the selected DOM and its descendants.",
66
"scripts": {

src/util/generateRulesAll.ts

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,84 @@
1-
import traversalCSSRuleList from "./traversalCSSRuleList";
2-
import convTextToRules from "./convTextToRules";
3-
import {cssHelper} from "./cssHelper";
4-
import convUrlToAbs from "./convUrlToAbs";
1+
import traversalCSSRuleList from './traversalCSSRuleList'
2+
import convTextToRules from './convTextToRules'
3+
import { cssHelper } from './cssHelper'
4+
import convUrlToAbs from './convUrlToAbs'
55

6-
type cssNodeObj = Awaited<ReturnType<typeof convTextToRules>>;
6+
type cssNodeObj = Awaited<ReturnType<typeof convTextToRules>>
77

88
function generateRulesAll(
99
doc: Document,
10-
externalCssCache: { [index: cssNodeObj["href"]]: cssNodeObj }
10+
externalCssCache: { [index: cssNodeObj['href']]: cssNodeObj }
1111
) {
12-
var x: number;
12+
var x: number
1313

1414
var objCss = {
1515
normRule: [],
1616
fontFace: [],
1717
keyFram: [],
18-
};
18+
}
1919

20-
var promises = [];
20+
var promises = []
2121

2222
return new Promise(function (resolve, reject) {
2323
// loop every styleSheets
2424
for (x = 0; x < doc.styleSheets.length; x++) {
25-
const styleSheet = doc.styleSheets[x];
25+
const styleSheet = doc.styleSheets[x]
2626
promises.push(
2727
new Promise(function (res) {
28-
var cssNodeArr : cssNodeObj;
28+
var cssNodeArr: cssNodeObj
2929
if (styleSheet.href !== null) {
3030
// can be link tag
31-
cssNodeArr = externalCssCache[styleSheet.href];
32-
cssNodeArr.media = doc.styleSheets[x].media;
33-
traversalCSSRuleList(doc, externalCssCache, cssNodeArr).then(res);
34-
} else if(styleSheet.ownerNode instanceof Element) {
31+
cssNodeArr = externalCssCache[styleSheet.href]
32+
cssNodeArr.media = doc.styleSheets[x].media
33+
traversalCSSRuleList(doc, externalCssCache, cssNodeArr).then(res)
34+
} else if (styleSheet.ownerNode instanceof Element) {
3535
// style tag
36-
let html: string = styleSheet.ownerNode.innerHTML;
37-
if (html === "") {
36+
let html: string = styleSheet.ownerNode.innerHTML
37+
if (html === '') {
3838
// style may be in style-tag's cssRules but not show in innerHTML
3939
for (
4040
let index = 0;
4141
index < doc.styleSheets[x].cssRules.length;
4242
index++
4343
) {
44-
const rule = doc.styleSheets[x].cssRules[index];
45-
html += rule.cssText;
44+
const rule = doc.styleSheets[x].cssRules[index]
45+
html += rule.cssText
4646
}
4747
}
4848
// convert urls in style tag to abs
4949
html = html.replace(
5050
/url\((['"]?)(.*?)\1\)/g,
51-
function (_a, _p1, p2) {
52-
return "url(\"" + convUrlToAbs(doc.location.href, p2) + "\")";
51+
function (_a, p1, p2) {
52+
return (
53+
'url(' + p1 + convUrlToAbs(doc.location.href, p2) + p1 + ')'
54+
)
5355
}
54-
);
56+
)
5557
// the next operation is asynchronous
5658
// store the current x value
57-
let _x = x;
58-
convTextToRules(html, doc.location.href).then(cssNodeObj=>{
59-
cssNodeObj.media = doc.styleSheets[_x].media;
60-
traversalCSSRuleList(doc, externalCssCache, cssNodeObj).then(res);
61-
});
62-
}else{
59+
let _x = x
60+
convTextToRules(html, doc.location.href).then((cssNodeObj) => {
61+
cssNodeObj.media = doc.styleSheets[_x].media
62+
traversalCSSRuleList(doc, externalCssCache, cssNodeObj).then(res)
63+
})
64+
} else {
6365
// console.log('ProcessingInstruction', styleSheet.ownerNode);
6466
res({})
6567
}
6668
})
67-
);
69+
)
6870
}
6971

7072
Promise.all(promises)
7173
.then(function (result) {
7274
result.forEach(function (ele) {
73-
cssHelper.mergeobjCss(objCss, ele);
74-
});
75-
resolve(objCss);
75+
cssHelper.mergeobjCss(objCss, ele)
76+
})
77+
resolve(objCss)
7678
})
7779
.catch(function (err) {
78-
reject(err);
79-
});
80-
});
80+
reject(err)
81+
})
82+
})
8183
}
82-
export default generateRulesAll;
84+
export default generateRulesAll

0 commit comments

Comments
 (0)