-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
65 lines (61 loc) · 3.28 KB
/
Copy pathbackground.js
File metadata and controls
65 lines (61 loc) · 3.28 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
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
// Initialize default settings when extension is installed
chrome.runtime.onInstalled.addListener(function () {
chrome.storage.sync.set({
bionicEnabled: true,
boldRatio: 50
}, function () {
console.log('Default settings initialized');
});
});
// Listen for tab updates to reapply bionic reading when navigating
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (changeInfo.status === 'complete' && tab.url && !tab.url.startsWith('chrome://')) {
chrome.storage.sync.get(['bionicEnabled'], function (result) {
const isEnabled = result.bionicEnabled !== undefined ? result.bionicEnabled : true;
if (isEnabled) {
// Wait a moment for the page to fully load before applying bionic reading
setTimeout(() => {
chrome.tabs.sendMessage(tabId, { action: 'checkStatus' }, function (response) {
// If error or no response, the content script may not be injected yet
if (chrome.runtime.lastError || !response) {
console.log('Content script not loaded, injecting...');
try {
chrome.scripting.executeScript({
target: { tabId: tabId },
files: ['content.js']
}).then(() => {
chrome.scripting.insertCSS({
target: { tabId: tabId },
files: ['bionic.css']
}).then(() => {
// After injection, send toggle message to apply bionic reading
chrome.storage.sync.get(['boldRatio'], function (ratioResult) {
const ratio = ratioResult.boldRatio || 50;
chrome.tabs.sendMessage(tabId, {
action: 'toggleBionic',
enabled: isEnabled,
boldRatio: ratio
});
});
});
});
} catch (error) {
console.error("Error injecting content script:", error);
}
} else if (response && !response.bionicApplied && isEnabled) {
// Content script loaded but bionic not applied, apply it
chrome.storage.sync.get(['boldRatio'], function (ratioResult) {
const ratio = ratioResult.boldRatio || 50;
chrome.tabs.sendMessage(tabId, {
action: 'toggleBionic',
enabled: isEnabled,
boldRatio: ratio
});
});
}
});
}, 500);
}
});
}
});