Skip to content

Commit a8ab5c5

Browse files
committed
depends on storage
1 parent 42e227b commit a8ab5c5

File tree

1 file changed

+50
-22
lines changed

1 file changed

+50
-22
lines changed

js/scrapboxCssScript.js

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,6 @@ const insertIndentCSSRule = (scrapboxIndentOptions) => {
6464
});
6565
};
6666

67-
const makerAttachment = () => {
68-
chrome.storage.local.get('scrapboxIndentOption', (result) => {
69-
const scrapboxIndentOptions = result.scrapboxIndentOption;
70-
insertIndentCSSRule(scrapboxIndentOptions);
71-
});
72-
};
73-
74-
// update
75-
chrome.runtime.onMessage.addListener((request) => {
76-
if (request === 'scrapbox_list_maker') {
77-
makerAttachment();
78-
}
79-
});
80-
81-
// initialize
82-
makerAttachment();
83-
8467
const indentColorCSS = [
8568
`.indent-mark .char-index{
8669
--scrapbox-indent-maker-opacity: 0.2;
@@ -97,12 +80,57 @@ const indentColorCSS = [
9780
`.app:not(.presentation) .indent-mark .char-index:nth-last-child(1) { background-color: transparent !important; }`,
9881
];
9982

100-
const insertIndentColorCSSRule = () => {
83+
const insertIndentColorCSSRule = (isColoring) => {
84+
// delete existing rules
85+
const cssRules = document.styleSheets[0].cssRules;
86+
const cssRulesNum = cssRules.length;
87+
for (let i = cssRulesNum - 1; i >= 0; i--) {
88+
const cssRule = cssRules[i];
89+
const cssSelector = cssRule.selectorText;
90+
91+
if (cssSelector === undefined) continue;
92+
93+
if (
94+
cssSelector.match(
95+
/^\.app:not\(.presentation\) \.indent-mark \.char-index:nth-child.*/
96+
)
97+
) {
98+
document.styleSheets[0].deleteRule(i);
99+
}
100+
}
101+
101102
// insert new rules
102-
indentColorCSS.map((css) => {
103-
console.log(css);
104-
document.styleSheets[0].insertRule(css);
103+
if (isColoring) {
104+
indentColorCSS.map((css) => {
105+
document.styleSheets[0].insertRule(css);
106+
});
107+
}
108+
};
109+
110+
const makerAttachment = () => {
111+
chrome.storage.local.get('scrapboxIndentOption', (result) => {
112+
const scrapboxIndentOptions = result.scrapboxIndentOption;
113+
insertIndentCSSRule(scrapboxIndentOptions);
105114
});
106115
};
107116

108-
insertIndentColorCSSRule();
117+
const coloringAttachment = () => {
118+
chrome.storage.local.get('scrapboxIndentColoring', (result) => {
119+
const isColoring = result.scrapboxIndentColoring;
120+
insertIndentColorCSSRule(isColoring);
121+
});
122+
};
123+
124+
// update
125+
chrome.runtime.onMessage.addListener((request) => {
126+
if (request === 'scrapbox_list_maker') {
127+
makerAttachment();
128+
}
129+
if (request === 'scrapbox_indent_coloring') {
130+
coloringAttachment();
131+
}
132+
});
133+
134+
// initialize
135+
makerAttachment();
136+
coloringAttachment();

0 commit comments

Comments
 (0)