-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
79 lines (75 loc) · 2.22 KB
/
background.js
File metadata and controls
79 lines (75 loc) · 2.22 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
function generatePasta(tag) {
chrome.storage.sync.get(tag, function(items) {
var possibleOptions = items[tag];
console.log(possibleOptions);
if (!possibleOptions) {
var opt = {
type: "basic",
title: "Unknown Tag",
message: "Tag '" + tag + "' not found",
iconUrl: "icon.png"
}
chrome.notifications.create(opt);
}
var message =
possibleOptions[Math.floor(Math.random() * possibleOptions.length)];
if (message) {
copyToClipboard(message);
var opt = {
type: "basic",
title: "Copied to Clipboard",
message: "Message from tag '" + tag + "' copied",
iconUrl: "icon.png"
}
chrome.notifications.create(opt);
} else {
console.log("No message");
}
});
}
function copyToClipboard(message) {
const text = document.createElement("textarea");
text.style.position = "fixed";
text.style.opacity = 0;
text.value = message;
document.body.appendChild(text);
text.select();
document.execCommand("Copy");
document.body.removeChild(text);
}
chrome.commands.onCommand.addListener(function(command) {
console.log(command);
if (command === "Get Tag") {
if (!("webkitSpeechRecognition" in window)) {
upgrade();
}
else {
var recognition = new webkitSpeechRecognition();
recognition.lang = "en";
recognition.onresult = function(event) {
console.log(event);
if (event.results.length > 0) {
console.log(event.results[event.results.length-1]);
var result = event.results[event.results.length-1];
if(result.isFinal) {
var transcript = result[0].transcript.toUpperCase();
console.log(transcript);
generatePasta(transcript);
};
}
}
recognition.onerror = function(event) {
console.log("error: " + event.error);
if (event.error === "not-allowed") {
var newURL = "microphone.html";
chrome.tabs.create({ url: newURL });
}
};
recognition.onend = function(event) {
chrome.browserAction.setIcon({path: "icon.png"});
};
recognition.start();
chrome.browserAction.setIcon({path: "mic.png"});
};
}
});