-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
93 lines (74 loc) · 1.96 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
console.log('Background script running');
chrome.browserAction.onClicked.addListener(noteThis);
function noteThis(tab){
var text="Noted this text";
chrome.tabs.sendMessage(tab.id, text);
}
chrome.contextMenus.create({title: "Note It!", contexts:["selection"], onclick: noteThisText});
var notesArray = [];
var count = 0;
function noteThisText(info)
{
var selectedstring = info.selectionText;
var splitedArray= selectedstring.split(" ");
//console.log(splitedArray[0]);
var newString = '';
for(let i=0;i<splitedArray.length;i++){
newString = newString+' '+splitedArray[i];
if((i!=0) && (i%20==0)){
newString+='\n';
}
}
notesArray[count++] = newString;
selectedstring=newString;
chrome.storage.local.set({key: notesArray});
chrome.storage.local.get(['key'], function(result) {
console.log('Value currently is ' + result.key);
});
var blob = new Blob([selectedstring],
{type:"text/plain;charset=UTF-8"}
);
saveAs(blob,"Note.txt");
//console.log(notesArray);
post({msg:selectedstring});
}
async function post(data) {
try {
// Create request to api service
const req = await fetch('http://localhost:3000/api', {
method: 'POST',
headers: { 'Content-Type':'application/json' },
// format the data
body: JSON.stringify(data),
});
const res = await req.json();
// Log success message
console.log(res);
} catch(err) {
console.log(err.toString());
}
}
/*
function post(selectedstring){
console.log("posting!!");
const data = { selectedstring };
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/JSON'
},
body: JSON.stringify(data)
};
fetch('/api',options)
.then(function (data) {
console.log('Request succeeded with JSON response', data);
})
.catch(function (error) {
console.log('Request failed', error);
});
}
*/
/*
*/
/*
*/