-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
79 lines (68 loc) · 2.22 KB
/
background.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
let recognition;
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.command === 'startListening') {
startListening();
}
});
function startListening() {
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
recognition = new SpeechRecognition();
recognition.lang = 'en-US';
recognition.continuous = true;
recognition.interimResults = false;
recognition.onresult = (event) => {
const speechResult = event.results[event.results.length - 1][0].transcript.trim().toLowerCase();
console.log('Speech received: ' + speechResult);
if (speechResult.includes('scroll down')) {
scrollPage('down');
} else if (speechResult.includes('scroll up')) {
scrollPage('up');
} else if (speechResult.includes('click')) {
clickElement(speechResult.replace('click ', ''));
} else if (speechResult.includes('go to')) {
navigateTo(speechResult.replace('go to ', ''));
}
};
recognition.onerror = (event) => {
console.error(event.error);
recognition.stop();
};
recognition.onend = () => {
console.log('Speech recognition service disconnected');
};
recognition.start();
}
function scrollPage(direction) {
chrome.scripting.executeScript({
target: { tabId: getActiveTabId() },
function: (direction) => {
window.scrollBy(0, direction === 'down' ? window.innerHeight : -window.innerHeight);
},
args: [direction]
});
}
function clickElement(elementDescription) {
chrome.scripting.executeScript({
target: { tabId: getActiveTabId() },
function: (elementDescription) => {
const elements = document.querySelectorAll('a, button');
for (let element of elements) {
if (element.textContent.trim().toLowerCase().includes(elementDescription)) {
element.click();
break;
}
}
},
args: [elementDescription]
});
}
function navigateTo(url) {
chrome.tabs.update({ url: 'http://' + url });
}
function getActiveTabId() {
return new Promise((resolve) => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
resolve(tabs[0].id);
});
});
}