/
GA-AnnotationsManager.user.js
170 lines (155 loc) · 8.75 KB
/
GA-AnnotationsManager.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
// ==UserScript==
// @name Google Analytics - Manage profiles annotations
// @version 0.2.1
// @license GPLv3 : http://www.gnu.org/licenses/gpl-3.0.txt
// @author Vincent Giersch <mail@vincent.sh>
// @description Manage annotations of Google Analytics profiles (multiple copy, delete and CSV export). Works only on Google Analytics V5.
// @include https://www.google.com/analytics/*
// @require https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
// @require https://raw.github.com/gierschv/jQuery-plugin-fireEvent/master/jquery.fireEvent.js
// ==/UserScript==
(function() {
var dumpAnnotations = function() {
var dumpAnnotationsProcess = function() {
var annotationsArray = [];
$('#AnnotationsDrawer_list > tbody:eq(1) > tr').each(function(idx, elem) {
if ($(elem).find('input[type="checkbox"]').attr('checked') != undefined)
{
annotationsArray.push({
date: $(elem).find('.Ld').text(),
text: $(elem).find('.dr > span:eq(1)').text(),
isPrivate: $(elem).find('.dr > span.ng').css('display') == 'block'
});
}
});
if (annotationsArray.length == 0)
return;
localStorage.setItem('annotations', JSON.stringify(annotationsArray));
$('#AnnotationManagerWrapper').html(' | ' + annotationsArray.length + ' annotations are into localstorage.');
};
dumpAnnotationsProcess();
return false;
};
var showAnnotations = function() {
var annotationsArray = localStorage.getItem('annotations');
if (!annotationsArray)
return false;
var str = "List of annotations stored in localstorage :\n";
annotationsArray = JSON.parse(annotationsArray);
for (var i = 0 ; i < annotationsArray.length ; ++i)
{
str += annotationsArray[i].date;
if (annotationsArray[i].isPrivate)
str += " [private]"
str += ": " + annotationsArray[i].text + "\n";
}
alert(str);
return false;
};
var exportAnnotations = function() {
var outputCSV = "Date,Text,Creator,Is Private\r\n", line = "";
$('#AnnotationsDrawer_list > tbody:eq(1) > tr').each(function(idx, elem) {
if ($(elem).find('input[type="checkbox"]').attr('checked') != undefined)
{
line = '"' + $(elem).find('.Ld').text() + '"';
line += ',"' + $(elem).find('.dr > span:eq(1)').text().replace(/"/g, '""') + '"';
line += ',"' + $(elem).find('.Hx').text() + '"';
line += ',"' + ($(elem).find('.dr > span.ng').css('display') == 'block' ? 'Yes' : 'No') + '"';
line += "\r\n";
outputCSV += line;
}
});
window.open('data:text/csv;charset=utf8,' + encodeURIComponent(outputCSV));
};
var pasteAnnotationsFill = function(annotation)
{
var form = $('.EK > form');
$(form).find('input[name="date"]').val(annotation.date);
$(form).find('textarea[name="text"]').val(annotation.text);
if (annotation.isPrivate)
$(form).find('input[name="access"]').val('PRIVATE');
$(form).find('textarea[name="text"]').fireEvent('click', {button:1});
$(form).find('a.o.un').fireEvent('click', {button:1});
}
var pasteAnnotations = function() {
var annotationsArray = localStorage.getItem('annotations');
if (!annotationsArray)
return false;
annotationsArray = JSON.parse(annotationsArray);
var pasteAnnotationsCurrent = 0;
$(document).bind('DOMAttrModified', function(event) {
if (event.newValue == 'VR Li disabled')
{
pasteAnnotationsFill(annotationsArray[pasteAnnotationsCurrent]);
pasteAnnotationsCurrent++;
if (pasteAnnotationsCurrent == annotationsArray.length)
{
$(document).unbind('DOMAttrModified');
return true;
}
$('#AnnotationDrawer_controls').find('a.VR.Li').fireEvent('click', {button:1});
return true;
}
});
$('#AnnotationDrawer_controls').find('a.VR.Li').fireEvent('click', {button:1});
};
var cancelCpyAnnotations = function() {
localStorage.removeItem('annotations');
$('#AnnotationManagerWrapper').remove();
displayLinks();
};
var removeAnnotations = function() {
if (!confirm('Remove these annotations ?'))
return false;
unsafeWindow.orig_confirm = unsafeWindow.confirm;
unsafeWindow.confirm = function() { return true; };
$('#AnnotationsDrawer_list > tbody:eq(1) > tr').each(function(idx, elem) {
if ($(elem).find('input[type="checkbox"]').attr('checked') != undefined)
{
$(this).find('.ew > a').fireEvent('click', {button:1});
$('#AnnotationsDrawer_list').find('.Xq:visible').fireEvent('click', {button:1});
}
});
unsafeWindow.confirm = unsafeWindow.orig_confirm;
return true;
};
var displayLinks = function() {
$('#AnnotationManagerWrapper').remove();
$('.AnnotationManager_chckbx').remove();
var annotationsArray = localStorage.getItem('annotations');
if (annotationsArray && (annotationsArray = JSON.parse(annotationsArray).length) > 0)
{
$('#AnnotationDrawer_controls').find('a.Hi.LS').parent().append('<span id="AnnotationManagerWrapper">\
| <a id="AnnotationManager_paste" class="Hi" onclick="return false;" href="#">Paste ' +
annotationsArray + ' annotation(s)</a>\
| <a id="AnnotationManager_show" class="Hi" onclick="return false;" href="#">Show annotation(s) in localstorage</a>\
| <a id="AnnotationManager_cancelcpy" class="Hi" onclick="return false;" href="#">Cancel copy</a>\
</span>');
$('#AnnotationManager_paste').click(pasteAnnotations);
$('#AnnotationManager_show').click(showAnnotations);
$('#AnnotationManager_cancelcpy').click(cancelCpyAnnotations);
return true;
}
$('#AnnotationDrawer_controls').find('a.Hi.LS').parent().append('<span id="AnnotationManagerWrapper">\
| <a id="AnnotationManager_copy" class="Hi" onclick="return false;" href="#">Copy annotation(s)</a>\
| <a id="AnnotationManager_remove" class="Hi" onclick="return false;" href="#">Remove annotation(s)</a>\
| <a id="AnnotationManager_export" class="Hi" onclick="return false;" href="#">Export annotation(s) as CSV</a>\
| \
Select :\
<a id="AnnotationManager_select_all" class="Hi" onclick="return false;" href="#">All</a>\
| <a id="AnnotationManager_select_none" class="Hi" onclick="return false;" href="#">None</a>\
</span>');
$('#AnnotationsDrawer_list > tbody:eq(1) > tr').append('<td class="AnnotationManager_chckbx"><input type="checkbox" /></td>');
$('#AnnotationManager_select_all').click(function(){ $('.AnnotationManager_chckbx > input[type="checkbox"]').attr('checked', true); });
$('#AnnotationManager_select_none').click(function(){ $('.AnnotationManager_chckbx > input[type="checkbox"]').attr('checked', false); });
$('#AnnotationManager_copy').click(dumpAnnotations);
$('#AnnotationManager_remove').click(removeAnnotations);
$('#AnnotationManager_export').click(exportAnnotations);
return true;
};
$(document).bind('DOMNodeInserted', function(event) {
if (event.target.id.indexOf('AnnotationDrawer') >= 0 || /* Init */
event.target.className.indexOf('Vp editable') >= 0) /* New element */
displayLinks();
});
})();