-
Notifications
You must be signed in to change notification settings - Fork 0
/
adblock-uiscripts-load_wizard_resources.js
98 lines (87 loc) · 5.08 KB
/
adblock-uiscripts-load_wizard_resources.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* This file is part of AdBlock <https://getadblock.com/>,
* Copyright (C) 2013-present Adblock, Inc.
*
* AdBlock is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* AdBlock is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AdBlock. If not, see <http://www.gnu.org/licenses/>.
*/
/* For ESLint: List any global identifiers used in this file below */
/* global browser */
// Binds keypress enter to trigger click action on
// default button or trigger click action on focused
// button.
/* eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars */
function bindEnterClickToDefault($dialog) {
if (window.GLOBAL_BIND_ENTER_CLICK_TO_DEFAULT) {
return;
}
window.GLOBAL_BIND_ENTER_CLICK_TO_DEFAULT = true;
$('html').on('keypress', (e) => {
if (e.keyCode === 13 && !$('button:focus').length) {
e.preventDefault();
$dialog.find('.adblock-default-button').filter(':visible').trigger('click');
}
});
}
// Inputs:
// - $base : jQuery Element to attach the CSS as children
// - callback : function to call when loading is complete
/* eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars */
function loadWizardResources($base, callback) {
function loadCss(cssSrc) {
const cssUrl = browser.runtime.getURL(cssSrc);
const fontCssUrl = browser.runtime.getURL('fonts/font-face.css');
const $styleTag = $('<style />').addClass('adblock-ui-stylesheet');
// HTML element <link> is ignored in shadow tree in Chrome 53
// so we must load the CSS file in some other ways e.g. using <style>
fetch(cssUrl).then(response => response.text()).then((wizardCssRules) => {
$styleTag.text(`${$styleTag.text()}${wizardCssRules}`);
});
fetch(fontCssUrl).then(response => response.text()).then((fontFaceRules) => {
$styleTag.text(`${$styleTag.text()}${fontFaceRules}`);
});
$base.append($styleTag);
}
function loadFont(name, style, weight, unicodeRange) {
return new FontFace('Lato', `url(${browser.runtime.getURL(`/fonts/${name}.woff`)}`, { style, weight, unicodeRange });
}
loadCss('adblock-uiscripts-adblock-wizard.css');
// load fonts programmatically
// Referencing the fonts in CSS do not load the fonts properly (reason unknown)
// but programmatically loading them performs reliably.
const fonts = [];
fonts.push(loadFont('lato-regular', 'normal', 'normal', 'U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD'));
fonts.push(loadFont('lato-ext-regular', 'normal', 'normal', 'U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF'));
fonts.push(loadFont('lato-ext-italic', 'italic', 'normal', 'U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF'));
fonts.push(loadFont('lato-italic', 'italic', 'normal', 'U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD'));
fonts.push(loadFont('lato-ext-bolditalic', 'italic', 'bold', 'U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF'));
fonts.push(loadFont('lato-bolditalic', 'italic', 'bold', 'U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD'));
fonts.push(loadFont('lato-ext-bold', 'normal', 'bold', 'U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF'));
fonts.push(loadFont('lato-bold', 'normal', 'bold', 'U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD'));
fonts.push(new FontFace('Material Icons', `url(${browser.runtime.getURL('/icons/MaterialIcons-Regular.woff2')}`, { style: 'normal', weight: 'normal' }));
fonts.push(new FontFace('AdBlock Icons', `url(${browser.runtime.getURL('/icons/adblock-icons.woff2')}`, { style: 'normal', weight: 'normal' }));
Promise.all(fonts).then((loaded) => {
for (let i = 0; i < loaded.length; i++) {
// documents.fonts supported in Chrome 60+ and documents.fonts.add() is experimental
// https://developer.mozilla.org/en-US/docs/Web/API/FontFaceSet#Browser_compatibility
// https://developer.mozilla.org/en-US/docs/Web/API/Document/fonts#Browser_compatibility
document.fonts.add(loaded[i]);
}
callback();
}).catch(() => {
callback();
});
}
// required return value for tabs.executeScript
/* eslint-disable-next-line no-unused-expressions */
'';
//# sourceURL=/uiscripts/load_wizard_resources.js