-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.js
62 lines (59 loc) · 1.65 KB
/
core.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
console.log("Entered");
var list=document.getElementsByTagName("pre");
for(var i in list)
{
var button = document.createElement("button");
button.id="codesnap_"+i.toString();
button.addEventListener('click', function()
{
copyclick(document.getElementById(this.id).nextElementSibling);
});
var textnode = document.createTextNode("Copy");
button.appendChild(textnode);
if(list[i].parentNode!=null &&list[i].hasChildNodes())
list[i].parentNode.insertBefore(button,list[i]);
}
function copyclick(ele)
{
window.getSelection().removeAllRanges();
var range = document.createRange();
range.selectNode(ele)
window.getSelection().addRange(range);
try
{
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copy ' + msg);
saveLocal(ele.textContent);
}
catch(err)
{
console.log('unable to copy');
}
window.getSelection().removeAllRanges();
}
function saveLocal(text)
{
var date= new Date();
var datestr=date.getDate()+" "+(date.getMonth()+1)+" "+date.getFullYear()+" "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
chrome.storage.sync.get({"clip":"none"},function(items){
if(items["clip"].toString()=="none")
{
var clip=[{code:text,timecopy:datestr}];
chrome.storage.sync.set({ "clip": clip }, function(){
console.log("saved");
});
}
else
{
var arr=items["clip"];
if(arr.unshift({code:text,timecopy:datestr})>10)
{
arr.pop();
}
chrome.storage.sync.set({ "clip": arr }, function(){
console.log("saved");
});
}
});
}