-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextensionScript.js
More file actions
executable file
·160 lines (136 loc) · 4.4 KB
/
Copy pathextensionScript.js
File metadata and controls
executable file
·160 lines (136 loc) · 4.4 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
var urls = [];
var sessions = {};
var UL_CLASS = "dropdown-content";
var UL_ID = "dropdown";
// Functions
var session_save = function () {
// Send a message to the active tab
chrome.tabs.getAllInWindow(null
, function (tabs) {
tabs.forEach(function (tab) {
urls.push(tab.url);
});
var title = document.getElementById('name1').value;
var session = {};
session[title] = urls;
chrome.storage.local.set(session, function () {
});
if (sessions['names'] == "") {
sessions['names'] = [title];
} else if (sessions["names"].indexOf(title) > -1 === false) {
sessions['names'].push(title);
}
chrome.storage.local.set(sessions, function () {
location.reload();
});
});
};
var saveCurrentAndOpen = function (id) {
var name = prompt("enter name of session");
if (name == undefined || name == "") {
return;
}
document.getElementById('name1').value = name;
session_save();
discardCurrentAndOpen(id)
};
var discardCurrentAndOpen = function (id) {
chrome.windows.getCurrent(function(currentWindow) {
chrome.windows.remove(currentWindow.id,function() {
chrome.storage.local.get(id, function (data) {
var url = data[id];
chrome.windows.create({url: url})
})
})
})
};
var openAll = function (sessionName) {
chrome.storage.local.get(sessionName, function (data) {
var url = data[sessionName];
var l = url.length;
if( l > 0) {
chrome.windows.create({url: url})
}
})
};
var deleteSession = function (sessionName) {
console.log(sessionName)
var index = sessions["names"].indexOf(sessionName);
sessions["names"].splice(index,1);
chrome.storage.local.set(sessions, function () {
location.reload();
});
};
chrome.storage.local.get("names", function (session) {
var length = 0;
if (session.names == undefined) {
sessions['names'] = "";
} else {
sessions = session;
length = sessions['names'].length;
}
for (var i = 0; i < length; i++) {
var sessionName = sessions['names'][i];
generateDropdown(sessionName);
}
// Injecting script
var script1 = document.createElement('script');
script1.setAttribute('src', 'js/materialize.min.js');
document.body.appendChild(script1);
});
var generateDropdown = function (sessionName) {
var con = document.getElementById('section1');
var ul1 = document.createElement('ul');
ul1.className = UL_CLASS;
ul1.id = makeid()
dropDownNames = ['save current & open', 'discard current & open', 'open', 'delete']
for(i = 1; i < 5; i++) {
var a = document.createElement('a');
a.id = sessionName + i;
a.appendChild(document.createTextNode(dropDownNames[ i-1 ]));
a.addEventListener('click', function () {
var functionInvokeId = this.id[ this.id.length - 1 ]
switch (parseInt(functionInvokeId)) {
case 1:
saveCurrentAndOpen(sessionName);
break;
case 2:
discardCurrentAndOpen(sessionName)
break;
case 3:
openAll(sessionName)
break;
case 4:
deleteSession(sessionName)
break;
default:
break;
}
});
var list = document.createElement('li');
list.appendChild(a);
ul1.appendChild(list);
}
var a5 = document.createElement('a');
a5.className = "btn dropdown-button";
a5.href = "#!";
a5.setAttribute('data-activates', ul1.id);
var i1 = document.createElement('i');
i1.className = "mdi-navigation-arrow-drop-down right";
a5.appendChild(document.createTextNode(sessionName));
a5.appendChild(i1);
con.appendChild(ul1);
con.appendChild(a5);
con.appendChild(document.createElement('br'));
con.appendChild(document.createElement('br'));
};
function makeid()
{
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 5; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
saveButton = document.getElementById('saveSession');
saveButton.onclick = session_save;