-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
97 lines (73 loc) · 2.15 KB
/
popup.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
94
95
96
97
/**
* Get the url of the current page
* @throws if the url does not contain youtube in it
* @param {function (string)} calback when the url is found
*/
function getCurrentUrl (callback) {
var queryInfo = {
active : true,
currentWindow : true
};
chrome.tabs.query(queryInfo, (tabs) => {
var tab = tabs[0];
var url = tab.url;
console.assert(typeof url == 'string', 'tab.url should be a string');
if(!url.includes('youtube.com')) {
setBody("This page isn't youtube!");
throw new Error("A non-youtube page");
}
callback(url);
});
}
/**
* Copies input onto clipboard
* @param val - value to be copied
*/
function copyToClipboard(val){
var dummy = document.createElement('input');
document.body.appendChild(dummy);
dummy.setAttribute("id","dummy_id");
dummy.setAttribute("value",val);
dummy.select();
document.execCommand("copy");
document.body.removeChild(dummy);
}
//simple function for checking if input is number
function isNumber(n) { return /^-?[\d.]+(?:e-?\d+)?$/.test(n); }
/**
* @throws if input is not a number
*/
function getMin(){
var min = document.getElementById('minute').value;
if(!isNumber(min)){
setBody("Min & Sec should be a number!");
throw new Error("Trying to input a non-number");
}
return min;
}
/**
* @throws if input is not a number
*/
function getSec(){
var sec = document.getElementById('second').value;
if(!isNumber(sec)){
setBody("Min & Sec should be a number!");
throw new Error("Trying to input a non-number");
}
return sec;
}
function setBody(val){
document.getElementsByTagName("body")[0].innerHTML = val;
}
document.addEventListener('DOMContentLoaded', () => {
getCurrentUrl((url) => {
var generate = document.getElementById('generate');
generate.addEventListener('click', () => {
var min = getMin();
var sec = getSec();
var newUrl = url + "#t=" + min + "m" + sec + "s";
setBody("Copied onto Clipboard");
copyToClipboard(newUrl);
});
});
});