forked from teknium1/font-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
166 lines (148 loc) · 5.14 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// popup.js
document.addEventListener('DOMContentLoaded', function () {
// Get references to the HTML elements
const fontSelect = document.getElementById('fontSelect');
const applyButton = document.getElementById('applyButton');
// Replace the existing fonts array with this:
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
];
// Add this function to read the config file
function readConfig() {
return fetch(chrome.runtime.getURL('config.ini'))
.then(response => response.text())
.then(text => {
const lines = text.split('\n');
const config = {};
lines.forEach(line => {
if (line.includes('=')) {
const [key, value] = line.split('=').map(part => part.trim());
config[key] = value;
}
});
return config;
});
}
// Modify the fetchGoogleFonts function
function fetchGoogleFonts() {
readConfig().then(config => {
const apiKey = config.API_KEY;
fetch(`https://www.googleapis.com/webfonts/v1/webfonts?key=${apiKey}`)
.then(response => response.json())
.then(data => {
const googleFonts = data.items.map(font => font.family);
fonts.push(...googleFonts);
populateDropdown();
})
.catch(error => console.error('Error fetching Google Fonts:', error));
});
}
// Modify the existing code to use this function
function populateDropdown() {
fontSelect.innerHTML = ''; // Clear existing options
fonts.forEach(function (font) {
const option = document.createElement('option');
option.value = font;
option.textContent = font;
fontSelect.appendChild(option);
});
}
// Call this function instead of directly populating the dropdown
fetchGoogleFonts();
// 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 or Google Font
if (webSafeFonts.includes(fontName)) {
// For web-safe fonts, no need to import
styleElement.textContent = `
* {
font-family: '${fontName}', sans-serif !important;
}
`;
} else {
// For Google Fonts, use their API
styleElement.textContent = `
@import url('https://fonts.googleapis.com/css2?family=${fontName.replace(' ', '+')}');
* {
font-family: '${fontName}', sans-serif !important;
}
`;
}
// Append the style element to the document head
document.head.appendChild(styleElement);
}
});