-
Notifications
You must be signed in to change notification settings - Fork 0
/
cross-pod-links.user.js
201 lines (189 loc) · 7 KB
/
cross-pod-links.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
/**
* This is a Greasemonkey script and must be run using a Greasemonkey-compatible browser.
*/
// ==UserScript==
// @name Cross-pod Links (for Diaspora)
// @version 0.1.6
// @namespace net.maymay.diaspora.cross
// @updateURL https://github.com/fabacab/cross-pod-links/raw/master/cross-pod-links.user.js
// @description Click a Diaspora post's permalink to copy a server-relative URL to your clipboard. Makes it easy to write links in your Diaspora posts that will work across pods when the people you are sharing with are on a different pod than you.
// @require https://code.jquery.com/jquery-1.10.2.min.js
// TODO: Can the include be restricted based on a known podlist?
// @include https://*/*
// @grant GM_xmlhttpRequest
// @grant GM_addStyle
// @grant GM_log
// @grant GM_setClipboard
// ==/UserScript==
D_PERMALINKER = {};
D_PERMALINKER.CONFIG = {
'debug': false // switch to true to debug.
};
// Utility debugging function.
D_PERMALINKER.log = function (msg) {
if (!D_PERMALINKER.CONFIG.debug) { return; }
GM_log('CROSS-POD LINKS: ' + msg);
};
// Initializations.
GM_addStyle('\
.cross-pod-links-popup.tooltip.bottom .tooltip-arrow {\
left: 35%;\
}\
.cross-pod-links-popup .tooltip-inner {\
text-align: left;\
max-width: 100%;\
padding: 10px;\
}\
.cross-pod-links-popup button {\
float: right;\
margin: 0 0 10px 10px;\
opacity: 1;\
background: #FFF;\
color: #000;\
}\
.cross-pod-links-popup .tooltip-inner a {\
font-size: larger;\
display: block;\
clear: both;\
text-align: center;\
}\
.cross-pod-links-popup .tooltip-inner a:hover {\
text-decoration: underline;\
}\
');
D_PERMALINKER.init = function () {
// Set up a Mutation Observer to listen for new stream contents.
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
var observer = new MutationObserver(function (mutations) {
D_PERMALINKER.log('Observing mutations:');
mutations.forEach(function (mutation) {
var links = mutation.target.querySelectorAll('a[href]');
for (var i = 0; i < links.length; i++) {
if (links[i].pathname && links[i].pathname.match(/^\/posts\/[0-9a-f]+/)) {
D_PERMALINKER.main(links[i]);
}
}
});
});
var el = document.getElementById('main_stream')
|| document.getElementById('container');
// TODO: Figure out how to get this working for the notification dropdown, too.
//|| document.getElementById('notification_dropdown');
try {
observer.observe(el, {
'childList': true,
'subtree': true
});
} catch (e) {
console.log(e);
}
};
window.addEventListener('DOMContentLoaded', D_PERMALINKER.init);
D_PERMALINKER.setPermalinkGUID = function (anchor_node, guid) {
// translate the post URL on this pod
// to a post URL for a server-relative cross-pod GUID
var new_url = '/posts/' + guid;
if (anchor_node.search) {
new_url += anchor_node.search;
}
if (anchor_node.hash) {
new_url += anchor_node.hash;
}
anchor_node.setAttribute('href', new_url);
};
D_PERMALINKER.getCrossPodUrl = function (anchor_node) {
var new_url = anchor_node.pathname;
if (anchor_node.search) {
new_url += anchor_node.search;
}
if (anchor_node.hash) {
new_url += anchor_node.hash;
}
return new_url;
};
D_PERMALINKER.getPostRoot = function (node) {
var el = jQuery(node).closest('.stream-element');
if (0 === el.length) {
el = jQuery(node).closest('#single-post-container');
}
if (undefined === el.get(0)) {
D_PERMALINKER.log('No post root element found for node ' + node);
return false;
} else {
return el.get(0);
}
}
D_PERMALINKER.setDataAttributes = function (anchor_node, data) {
el = jQuery(D_PERMALINKER.getPostRoot(anchor_node));
el.attr('data-post-title', data.title); // Why won't .data() work here?
};
D_PERMALINKER.updatePermalink = function (anchor_node) {
var url = window.location.protocol + '//' + window.location.host + anchor_node.pathname + '.json';
GM_xmlhttpRequest({
'method': 'GET',
'url': url,
'onload': function (response) {
try {
var json = JSON.parse(response.responseText);
} catch (e) {
D_PERMALINKER.log('Error parsing received JSON from ' + url);
D_PERMALINKER.log(response.responseText);
}
D_PERMALINKER.setPermalinkGUID(anchor_node, json.guid);
D_PERMALINKER.setDataAttributes(anchor_node, json);
}
});
};
D_PERMALINKER.createSimplePopUp = function (node) {
var div = document.createElement('div');
div.setAttribute('class', 'cross-pod-links-popup tooltip fade bottom in');
var html = '';
html += '<div class="tooltip-arrow"></div>';
html += '<div class="tooltip-inner">';
html += '<button class="close" onclick="this.parentNode.parentNode.parentNode.removeChild(document.querySelector(\'.cross-pod-links-popup\'));">[X]</button>';
html += '<button class="cross-pod-links-pbcopy">Copy to Clipboard</button>';
html += '<button class="cross-pod-links-pbcopy-markdown">Copy as Markdown</button>';
html += '<a href="' + D_PERMALINKER.getCrossPodUrl(node) + '">';
html += 'Cross-pod Permalink: ' + D_PERMALINKER.getCrossPodUrl(node);
html += '</a>';
html += '</div>';
div.innerHTML = html;
jQuery(div.querySelector('button.cross-pod-links-pbcopy')).on('click', function (e) {
GM_setClipboard(D_PERMALINKER.getCrossPodUrl(node));
});
jQuery(div.querySelector('button.cross-pod-links-pbcopy-markdown')).on('click', function (e) {
GM_setClipboard('[' + D_PERMALINKER.getPostRoot(node).getAttribute('data-post-title') + '](' + D_PERMALINKER.getCrossPodUrl(node) + ')');
});
node.parentNode.appendChild(div);
};
D_PERMALINKER.isPermalink = function (node) {
var is_permalink = true;
var exclude_selectors = [
'.toggle_post_comments',
'.comment_delete',
'.control-icons a',
'.focus-comment',
'.like',
'.reshare',
'.post_report'
];
if (jQuery(node).is(exclude_selectors.join(', '))) {
is_permalink = false;
}
return is_permalink;
};
// This is the main() function, executed on page load.
D_PERMALINKER.main = function (anchor_node) {
if (!D_PERMALINKER.isPermalink(anchor_node)) { return; }
D_PERMALINKER.updatePermalink(anchor_node);
// attach a click listener on this permalink anchor element
if (!jQuery(anchor_node).closest('.cross-pod-links-popup').length) {
jQuery(anchor_node).unbind('click'); // not sure what was previously bound?
jQuery(anchor_node).on('click', function (e) {
e.preventDefault();
e.stopPropagation();
// display a small "tooltip" popup over the link
D_PERMALINKER.createSimplePopUp(anchor_node);
});
}
};