-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbookmarkstore.user.js
309 lines (281 loc) · 13.8 KB
/
bookmarkstore.user.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// ==UserScript==
// @id iitc-plugin-bookmarkstore
// @name IITC plugin: bookmarkstore
// @category Info
// @version 1.0.0.20221113.2221
// @namespace https://github.com/jonatkins/ingress-intel-total-conversion
// @updateURL https://raw.githubusercontent.com/Jormund/bookmarkstore/master/bookmarkstore.meta.js
// @downloadURL https://raw.githubusercontent.com/Jormund/bookmarkstore/master/bookmarkstore.user.js
// @description [2022-11-13-2221] Bookmarkstore
// @include https://*.ingress.com/intel*
// @include https://intel.ingress.com/*
// @match https://intel.ingress.com/*
// @match https://*.ingress.com/intel*
// @grant none
// ==/UserScript==
//Changelog
//1.0.0: Activate on intel-x.ingress.com
//0.1.1: Activate on intel.ingress.com, changed download url to github
//0.1.0: Added Opt menu with Import / Export buttons
function wrapper(plugin_info) {
if (typeof window.plugin !== 'function') window.plugin = function () { };
// PLUGIN START ////////////////////////////////////////////////////////
// use own namespace for plugin
window.plugin.bookmarkstore = function () { };
window.plugin.bookmarkstore.KEY_STORAGE = 'bookmarkstore-storage';
window.plugin.bookmarkstore.storage = {};
window.plugin.bookmarkstore.datas_bkmrk = '';
// update the localStorage datas
window.plugin.bookmarkstore.saveStorage = function () {
localStorage[window.plugin.bookmarkstore.KEY_STORAGE] = JSON.stringify(window.plugin.bookmarkstore.storage);
};
// load the localStorage datas
window.plugin.bookmarkstore.loadStorage = function () {
if (localStorage[window.plugin.bookmarkstore.KEY_STORAGE]) {
window.plugin.bookmarkstore.storage = JSON.parse(localStorage[window.plugin.bookmarkstore.KEY_STORAGE]);
}
};
// read bookmarks datas
window.plugin.bookmarkstore.getBookmarks = function () {
if (localStorage[window.plugin.bookmarks.KEY_STORAGE]) {
window.plugin.bookmarkstore.datas_bkmrk = localStorage[window.plugin.bookmarks.KEY_STORAGE];
return true;
}
return false; //window.plugin.bookmarks.bkmrksObj
};
// set bookmarks datas
window.plugin.bookmarkstore.setBookmarks = function () {
localStorage[window.plugin.bookmarks.KEY_STORAGE] = window.plugin.bookmarkstore.datas_bkmrk;
};
// change bookmarks from store
window.plugin.bookmarkstore.selectStoredBookmarks = function () {
var bmrkPrjct_name = $('#changeBookmarksButton').val();
if ($.trim(bmrkPrjct_name) === '') {
return false;
}
if (typeof window.plugin.bookmarkstore.storage[bmrkPrjct_name] === 'undefined') {
alert('project not found in store');
return false;
}
window.plugin.bookmarkstore.datas_bkmrk = JSON.stringify(window.plugin.bookmarkstore.storage[bmrkPrjct_name]);
window.plugin.bookmarkstore.setBookmarks();
window.plugin.bookmarks.refreshBkmrks();
window.runHooks('pluginBkmrksEdit', { "target": "all", "action": "import" });
};
window.plugin.bookmarkstore.resetBookmarks = function () {
delete localStorage[window.plugin.bookmarks.KEY_STORAGE];
window.plugin.bookmarks.createStorage();
window.plugin.bookmarks.loadStorage();
window.plugin.bookmarks.refreshBkmrks();
window.runHooks('pluginBkmrksEdit', { "target": "all", "action": "reset" });
};
// remove bookmarks from store
window.plugin.bookmarkstore.removeBookmarks = function () {
var bmrkPrjct_name = $('#changeBookmarksButton').val();
if ($.trim(bmrkPrjct_name) === '') {
window.plugin.bookmarkstore.resetBookmarks();
return false;
}
if (typeof window.plugin.bookmarkstore.storage[bmrkPrjct_name] === 'undefined') {
alert('project not found in store');
return false;
}
delete window.plugin.bookmarkstore.storage[bmrkPrjct_name];
window.plugin.bookmarkstore.saveStorage();
window.plugin.bookmarkstore.refreshMenu();
window.plugin.bookmarkstore.resetBookmarks();
};
// save bookmarks to store
window.plugin.bookmarkstore.saveBookmarks = function () {
var html = '<div class=""><div>Give a project name</div>name : <input id="bmrkPrjct_name" type="text"></input></div>';
dialog({
html: html,
id: 'plugin-bookmarkstore-new-name',
dialogClass: '',
title: 'new bookmarks project name',
buttons: {
'OK': function () {
var new_name = $('#bmrkPrjct_name').val();
if ($.trim(new_name) === '') {
alert('project name required');
return false;
}
var rexp = /^[\+0-9a-zA-Z_-]+$/;
if (rexp.test(new_name)) {
if (window.plugin.bookmarkstore.storage[new_name] !== undefined) {
if (!confirm('name already exists, do you want to overwrite this project ?')) {
return false;
}
}
if (window.plugin.bookmarkstore.getBookmarks()) {
window.plugin.bookmarkstore.storage[new_name] = JSON.parse(window.plugin.bookmarkstore.datas_bkmrk);
window.plugin.bookmarkstore.saveStorage();
window.plugin.bookmarkstore.refreshMenu();
} else {
alert('no bookmarks to save');
}
} else {
alert('alphanumeric string only');
return false;
}
$(this).dialog('close');
},
'Cancel': function () {
$(this).dialog('close');
}
}
});
};
// populate select menu
window.plugin.bookmarkstore.refreshMenu = function () {
window.plugin.bookmarkstore.loadStorage();
$('#changeBookmarksButton').find('option').remove();
$('#changeBookmarksButton').append($('<option>', { value: '', text: 'Select a project' }));
if (Object.keys(window.plugin.bookmarkstore.storage).length) {
$.each(window.plugin.bookmarkstore.storage, function (k, r) {
$('#changeBookmarksButton').append($('<option>', { value: k, text: k }));
});
}
};
// open opt dialog
window.plugin.bookmarkstore.openOpt = function() {
dialog({
html: window.plugin.bookmarkstore.htmlSetbox,
dialogClass: 'ui-dialog-bkmrksSet',
title: 'Bookmarkstore Options'
});
//window.runHooks('pluginBkmrksOpenOpt');
}
// reset store
window.plugin.bookmarkstore.optReset = function() {
var promptAction = confirm('All bookmark projects will be deleted. Are you sure?', '');
if(promptAction) {
delete localStorage[window.plugin.bookmarkstore.KEY_STORAGE];
window.plugin.bookmarkstore.storage = {};
window.plugin.bookmarkstore.saveStorage();
window.plugin.bookmarkstore.refreshMenu();
//window.runHooks('pluginBkmrksEdit', {"target": "all", "action": "reset"});
console.log('BOOKMARKSTORE: reset all bookmarks');
window.plugin.bookmarks.optAlert('Successful. ');
}
}
// copy bookmarks for export
window.plugin.bookmarkstore.optCopy = function() {
if(typeof android !== 'undefined' && android && android.shareString) {
return android.shareString(localStorage[window.plugin.bookmarkstore.KEY_STORAGE]);
} else {
dialog({
html: '<p><a onclick="$(\'.ui-dialog-bkmrksSet-copy textarea\').select();">Select all</a> and press CTRL+C to copy it.</p>'+
'<textarea readonly>'+
localStorage[window.plugin.bookmarkstore.KEY_STORAGE]+
'</textarea>',
dialogClass: 'ui-dialog-bkmrksSet-copy',
title: 'Bookmarkstore Export'
});
}
}
window.plugin.bookmarkstore.optExport = function() {
if(typeof android !== 'undefined' && android && android.saveFile) {
android.saveFile("IITC-bookmarkstore.json", "application/json", localStorage[window.plugin.bookmarkstore.KEY_STORAGE]);
}
}
// import bookmarks via paste
window.plugin.bookmarkstore.optPaste = function() {
var promptAction = prompt('Press CTRL+V to paste it.', '');
if(promptAction !== null && promptAction !== '') {
try {
JSON.parse(promptAction); // try to parse JSON first
localStorage[window.plugin.bookmarkstore.KEY_STORAGE] = promptAction;
//window.plugin.bookmarks.refreshBkmrks();//do not clear or change current bookmarks, user might want to add them to the store
window.plugin.bookmarkstore.refreshMenu();//we do refresh the dropdownlist
//window.runHooks('pluginBkmrksEdit', {"target": "all", "action": "import"});
console.log('BOOKMARKSTORE: reset and imported bookmarks');
window.plugin.bookmarks.optAlert('Successful. ');
} catch(e) {
console.warn('BOOKMARKSTORE: failed to import data: '+e);
window.plugin.bookmarks.optAlert('<span style="color: #f88">Import failed </span>');
}
}
}
window.plugin.bookmarkstore.optImport = function() {
if (window.requestFile === undefined) return;
window.requestFile(function(filename, content) {
try {
JSON.parse(content); // try to parse JSON first
localStorage[window.plugin.bookmarkstore.KEY_STORAGE] = promptAction;
//window.plugin.bookmarks.refreshBkmrks();//do not clear or change current bookmarks, user might want to add them to the store
window.plugin.bookmarkstore.refreshMenu();//we do refresh the dropdownlist
//window.runHooks('pluginBkmrksEdit', {"target": "all", "action": "import"});
console.log('BOOKMARKSTORE: reset and imported bookmarks');
window.plugin.bookmarks.optAlert('Successful. ');
} catch(e) {
console.warn('BOOKMARKS: failed to import data: '+e);
window.plugin.bookmarks.optAlert('<span style="color: #f88">Import failed </span>');
}
});
}
// init setup
window.plugin.bookmarkstore.setup = function () {
if (!window.plugin.bookmarks) {
console.log('**** bookmarkstore : not loaded, bookmarks is missing ****');
alert('Bookmarks plugin is required');
return;
}
window.plugin.bookmarkstore.setupCSS();
window.plugin.bookmarkstore.addPanel();
console.log('**** bookmarkstore : loaded ****');
};
window.plugin.bookmarkstore.setupCSS = function() {
$('<style>').prop('type', 'text/css').html('#bkmrkstoreSetbox a{'+
'display:block;'+
'color:#ffce00;'+
'border:1px solid #ffce00;'+
'padding:3px 0;'+
'margin:10px auto;'+
'width:80%;'+
'text-align:center;'+
'background:rgba(8,48,78,.9);'+
'}'+
'#bkmrkstoreSetbox a.disabled, #bkmrkstoreSetbox a.disabled:hover{'+
'color:#666;'+
'border-color:#666;'+
'text-decoration:none;}'+
'#bkmrkstoreSetbox{text-align:center;}')
.appendTo('head');
}
// toolbox menu
window.plugin.bookmarkstore.addPanel = function () {
var actions = '';
actions += '<a onclick="window.plugin.bookmarkstore.optReset();return false;">Reset bookmarkstore</a>';
actions += '<a onclick="window.plugin.bookmarkstore.optCopy();return false;">Copy bookmarkstore</a>';
actions += '<a onclick="window.plugin.bookmarkstore.optPaste();return false;">Paste bookmarkstore</a>';
if(window.plugin.bookmarks.isAndroid()) {
actions += '<a onclick="window.plugin.bookmarkstore.optImport();return false;">Import bookmarkstore</a>';
actions += '<a onclick="window.plugin.bookmarkstore.optExport();return false;">Export bookmarkstore</a>';
}
window.plugin.bookmarkstore.htmlSetbox = '<div id="bkmrkstoreSetbox">' + actions + '</div>';
$('#toolbox').after('<div id="bookmarkstore-toolbox" style="padding:3px;"></div>');
$('#bookmarkstore-toolbox')
.append(' <strong>Bookmarks : </strong><select onchange="window.plugin.bookmarkstore.selectStoredBookmarks()" id="changeBookmarksButton" title="Change Bookmarks"></select><br />')
.append(' <a onclick="window.plugin.bookmarkstore.saveBookmarks()">Save</a> ')
.append(' <a onclick="window.plugin.bookmarkstore.removeBookmarks()">Delete</a> ')
.append(' <a onclick="window.plugin.bookmarkstore.resetBookmarks()">Clear bookmarks</a>')
.append(' <a onclick="window.plugin.bookmarkstore.openOpt()">Opt</a>');
window.plugin.bookmarkstore.refreshMenu();
};
// runrun
var setup = window.plugin.bookmarkstore.setup;
setup.info = plugin_info; //add the script info data to the function as a property
if (!window.bootPlugins) window.bootPlugins = [];
window.bootPlugins.push(setup);
// if IITC has already booted, immediately run the 'setup' function
if (window.iitcLoaded && typeof setup === 'function') {
setup();
}
// PLUGIN END ////////////////////////////////////////////////////////
} // WRAPPER END ////////////////////////////////////////////////////////
var script = document.createElement('script');
var info = {};
if (typeof GM_info !== 'undefined' && GM_info && GM_info.script) info.script = { version: GM_info.script.version, name: GM_info.script.name, description: GM_info.script.description };
script.appendChild(document.createTextNode('(' + wrapper + ')(' + JSON.stringify(info) + ');'));
(document.body || document.head || document.documentElement).appendChild(script);