-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpreload-fonts.js
More file actions
31 lines (30 loc) · 1.33 KB
/
preload-fonts.js
File metadata and controls
31 lines (30 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
(() => {
createPreloadTag(document); // ルートドキュメントにフォント読み込み
const iframes = document.getElementsByTagName('iframe');
for (iframe of iframes) {
createPreloadTag(iframe.contentDocument); // iframe内のドキュメントにフォント読み込み
};
})();
function createPreloadTag(elem) {
if (!elem) return;
const baseClassName = 'preloadFontTag';
const randomInt = Math.floor(Math.random() * 10000) // クラス名の被りを防止するための乱数
const className = baseClassName + randomInt;
const weightList = ['Regular', 'Bold'];
weightList.map((weight) => {
const preloadTag = elem.createElement('link');
preloadTag.setAttribute('rel', 'preload');
preloadTag.setAttribute('as', 'font');
preloadTag.setAttribute('href', chrome.runtime.getURL(`fonts/NotoSansCJKjp-${weight}-subset.woff2`));
preloadTag.setAttribute('crossorigin', true);
preloadTag.classList = className;
elem.body.appendChild(preloadTag);
preloadTag.addEventListener('load', () => {
const loadCSS = elem.createElement('link');
loadCSS.setAttribute('rel', 'stylesheet');
loadCSS.setAttribute('type', 'text/css');
loadCSS.setAttribute('href', chrome.runtime.getURL(`css/replacefont-extension-${weight.toLowerCase()}.css`));
elem.body.appendChild(loadCSS);
});
});
}