-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground0.js
More file actions
105 lines (87 loc) · 3.21 KB
/
Copy pathbackground0.js
File metadata and controls
105 lines (87 loc) · 3.21 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
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
// background.js
// Omnibox command handler (top-level)
chrome.omnibox.onInputEntered.addListener((text) => {
const [prefix, ...queryParts] = text.trim().split(' ');
const query = queryParts.join(' ');
let url;
switch (prefix) {
case 'y':
url = `https://www.youtube.com/results?search_query=${encodeURIComponent(query)}`;
break;
case 'g':
url = `https://github.com/search?q=${encodeURIComponent(query)}`;
break;
case 'b':
url = `https://search.brave.com/search?q=${encodeURIComponent(query)}`;
break;
case 'w':
url = `https://en.wikipedia.org/wiki/Special:Search?search=${encodeURIComponent(query)}`;
break;
case 's':
url = `https://stackoverflow.com/search?q=${encodeURIComponent(query)}`;
break;
case 'd':
url = `https://duckduckgo.com/?q=${encodeURIComponent(query)}`;
break;
default:
url = `https://www.google.com/search?q=${encodeURIComponent(text)}`;
}
chrome.tabs.create({ url });
});
// Message listener for popup-initiated searches
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.action === "search") {
handleSearchRequest(request.query, request.url, sendResponse);
}
});
function handleSearchRequest(query, url, sendResponse) {
chrome.tabs.create({ url: url + encodeURIComponent(query) });
sendResponse({ message: "Search result opened in a new tab." });
}
// Suggest/Auto add found new search engine
// Show engines
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'suggestSearchEngine') {
console.log("Detected engine:", request.engine);
// Optional: store in storage or session for now
chrome.storage.local.set({ lastSuggestedEngine: request.engine });
// Show a badge with a dot or letter
chrome.action.setBadgeText({ text: "✓" });
chrome.action.setBadgeBackgroundColor({ color: "#4caf50" });
// Optional: remove badge after 5 seconds
setTimeout(() => {
chrome.action.setBadgeText({ text: "" });
}, 5000);
}
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "suggestSearchEngine") {
const newEngine = request.engine;
console.log("Detected engine:", newEngine);
// Show a badge (✓) for feedback
chrome.action.setBadgeText({ text: "✓" });
chrome.action.setBadgeBackgroundColor({ color: "#4caf50" });
// Auto-clear badge after 5 seconds
setTimeout(() => {
chrome.action.setBadgeText({ text: "" });
}, 5000);
// Save to local storage
chrome.storage.local.get({ engines: [] }, (data) => {
const engines = data.engines;
// Avoid duplicates
const exists = engines.some(e => e.url === newEngine.url);
if (!exists) {
engines.push(newEngine);
chrome.storage.local.set({ engines }, () => {
console.log("Engine added:", newEngine);
chrome.runtime.sendMessage({ action: "engineAdded", engine: newEngine });
});
} else {
console.log("Engine already exists:", newEngine);
}
});
sendResponse({ success: true });
}
});
// Test script runs log message to console
console.log('f8 bg script running');