-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpf_quick_quote.user.js
More file actions
147 lines (112 loc) · 4.16 KB
/
pf_quick_quote.user.js
File metadata and controls
147 lines (112 loc) · 4.16 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
// ==UserScript==
// @name ProgrammersForumQuickQuote
// @namespace http://programmersforum.ru/
// @version 0.71
// @description adds a button to quote selected text, also changes the reply/quote button to not reload page
// @author Alex P
// @include *programmersforum.ru/*
// @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js
// @grant none
// @downloadURL https://github.com/AlexP11223/ProgForumRuUserscripts/raw/master/pf_quick_quote.user.js
// ==/UserScript==
// @grant must be none, otherwise cannot access vB_Editor in Firefox
(function() {
'use strict';
if (window.quickQuoteInitialized || $('#quick_quote_btn').length)
return;
window.quickQuoteInitialized = true;
function addStyle(css) {
var head = document.getElementsByTagName('head')[0];
var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = css;
head.appendChild(style);
}
function getSelectedText() {
return window.getSelection().toString();
}
function onPostClicked(e) {
var selectedText = $.trim(getSelectedText());
var targetTag = e.target.nodeName.toLowerCase();
if (selectedText && targetTag !== 'textarea' && targetTag !== 'input') {
stopFadeOut();
qqBtn.css({ top: (e.pageY + 10) + 'px', left: e.pageX + 'px'});
qqBtn.show(50, function() {
restartFadeOut();
});
var postContainer = $(this).closest('table');
currPostId = postContainer.attr('id').replace('post', '');
currAuthorName = postContainer.find('.bigusername').first().text();
currSelectedText = selectedText;
}
else {
qqBtn.hide();
}
}
function appendText(text) {
if (vB_Editor[QR_EditorID].get_editor_contents().length > 0) {
text = '\n' + text;
}
vB_Editor[QR_EditorID].insert_text(text);
vB_Editor[QR_EditorID].collapse_selection_end();
}
function appendQuote(text) {
var nameQuoteChar = '';
if (currAuthorName.indexOf(']') !== -1)
nameQuoteChar = "'";
appendText('[QUOTE=' + nameQuoteChar + currAuthorName + ';' + currPostId + nameQuoteChar + ']' + text + '[/QUOTE]\n');
}
function quoteSelected() {
appendQuote(currSelectedText);
qqBtn.hide();
}
function quotePost(postQuoteUrl, progressIndicator) {
progressIndicator.show();
$.get(postQuoteUrl, function(response) {
var html = $.parseHTML(response);
var quote = $.trim($(html).find('#vB_Editor_001_textarea').text());
if (quote) {
appendText(quote);
}
}).done(function() {
progressIndicator.hide();
});
}
addStyle('.qq-btn { z-index: 999;' +
'position: absolute;' +
'border: 1px solid midnightblue;' +
'padding: 3px;' +
'font-weight: bold;' +
'cursor: pointer;' +
'background-color: lightyellow; }');
$('<div id="quick_quote_btn" class="smallfont qq-btn" style="display:none;">Цитировать</div>').prependTo($('body'));
var qqBtn = $('#quick_quote_btn');
qqBtn.click(quoteSelected);
qqBtn.hover(function() {
stopFadeOut();
},
function() {
restartFadeOut();
});
$('#posts').on('mouseup', 'div[id^="post_message"]', onPostClicked);
var currSelectedText = '';
var currPostId = '';
var currAuthorName = '';
var fadeOutTimer = undefined;
function stopFadeOut() {
clearTimeout(fadeOutTimer);
qqBtn.stop(true, true);
}
function restartFadeOut() {
stopFadeOut();
fadeOutTimer = setTimeout(function() {
qqBtn.fadeOut();
}, 3000);
}
$('#posts').find('a:has(img[src*="quote."])').click( function(e) {
e.preventDefault();
var url = $(this).attr('href');
var progressIndicator = $(this).prevAll('img[id^="progress"]').first();
quotePost(url, progressIndicator);
});
})();