-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.js
130 lines (114 loc) · 3.91 KB
/
popup.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// popup.js
document.addEventListener('DOMContentLoaded', function () {
// Get references to the HTML elements
const fontSelect = document.getElementById('fontSelect');
const applyButton = document.getElementById('applyButton');
// List of fonts to populate the dropdown menu
const fonts = [
'Arial',
'Verdana',
'Helvetica',
'Times New Roman',
'Georgia',
'Courier New',
'Roboto',
'Open Sans',
'Lato',
'Montserrat',
'Oswald',
'Source Sans Pro',
'Slabo 27px',
'Raleway',
'PT Sans',
'Merriweather'
// Add more fonts as desired
];
// Populate the dropdown menu with fonts
fonts.forEach(function (font) {
const option = document.createElement('option');
option.value = font;
option.textContent = font;
fontSelect.appendChild(option);
});
// Load the saved font preference (if any)
chrome.storage.local.get('selectedFont', function (data) {
if (data.selectedFont) {
fontSelect.value = data.selectedFont;
}
});
// Add a click event listener to the "Apply Font" button
applyButton.addEventListener('click', function () {
// Get the selected font from the dropdown menu
const selectedFont = fontSelect.value;
console.log('Selected font:', selectedFont);
// Save the selected font in chrome storage
chrome.storage.local.set({ selectedFont: selectedFont }, function () {
console.log('Font preference saved:', selectedFont);
// Inject a script into the page to apply the font
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
const tabId = tabs[0].id;
chrome.scripting.executeScript(
{
target: { tabId: tabId },
func: applyCustomFont,
args: [selectedFont],
},
function () {
if (chrome.runtime.lastError) {
console.error('Error injecting script:', chrome.runtime.lastError.message);
} else {
console.log('Font updated successfully');
}
}
);
});
// Close the popup window
window.close();
});
});
// The function to inject into the page
function applyCustomFont(fontName) {
const styleId = 'customFontStyle';
let styleElement = document.getElementById(styleId);
// Remove existing style element if it exists
if (styleElement) {
styleElement.parentNode.removeChild(styleElement);
}
// Create a new style element
styleElement = document.createElement('style');
styleElement.id = styleId;
// List of web-safe fonts
const webSafeFonts = [
'Arial',
'Verdana',
'Helvetica',
'Times New Roman',
'Georgia',
'Courier New',
'Tahoma',
'Trebuchet MS',
'Impact',
'Lucida Console'
];
// Determine the CSS code based on whether the font is web-safe
if (webSafeFonts.includes(fontName)) {
// For web-safe fonts, no need to import
styleElement.textContent = `
* {
font-family: '${fontName}', sans-serif !important;
}
`;
} else {
// For other fonts, import from Google Fonts
const fontNameEncoded = encodeURIComponent(fontName.replace(/ /g, '+'));
styleElement.textContent = `
@import url('https://fonts.googleapis.com/css2?family=${fontNameEncoded}&display=swap');
* {
font-family: '${fontName}', sans-serif !important;
}
`;
}
// Append the style element to the document head
document.head.appendChild(styleElement);
}
});