-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitbucket.js
80 lines (63 loc) · 2.17 KB
/
bitbucket.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
// ==UserScript==
// @name PhpStormRESTControl
// @version 0.2
// @description Opens a file from Bitbucket in your IDE! Adapt @match below to suit your needs.
// @author Michael Wölk
// @match https://bitbucket.org/*
// @require https://code.jquery.com/jquery-3.2.1.min.js
// @require https://cdnjs.cloudflare.com/ajax/libs/arrive/2.4.1/arrive.min.js
// ==/UserScript==
this.$ = this.jQuery = jQuery.noConflict(true);
(function () {
const msInitOnReady = 200;
const startPort = 8100;
const maxPort = 8110;
function send(port, file = '', line = 0) {
const url = 'http://localhost:' + port + '/?file=' + encodeURIComponent(file) + '&line=' + line;
return $.ajax({
url,
type: "GET",
port
});
}
function sendBatch(file = '', line = 0) {
for (const port = startPort; port <= maxPort; port++) {
send(port, file, line);
}
}
function init(elements) {
elements.each(function (i, sel) {
const span = '<span class="phpstormIcon" style="cursor: pointer;"><img src="https://upload.wikimedia.org/wikipedia/commons/c/c9/PhpStorm_Icon.svg" height="16"/></span>';
$(sel).not(':has(span.phpstormIcon)').prepend(span);
});
$('.phpstormIcon').click(function (event) {
event.stopPropagation();
let filename = $(this).parents('.changes-tree .file, .file-header').find('a').attr('href');
filename = filename.replace(/^.*#/, '');
if (filename === null) {
return;
}
const re = /\?t=(\d+)$/;
const lineMatch = re.exec(filename);
if (lineMatch !== null) {
const line = lineMatch[1];
filename = filename.replace(re, '');
sendBatch(filename, line);
} else {
sendBatch(filename);
}
});
}
function initOnReady() {
setTimeout(function tick() {
const elements = $('.changes-tree .file, .file-header .atlaskit-icon');
if (elements.length > 0) {
init(elements);
} else {
setTimeout(tick, msInitOnReady);
}
}, msInitOnReady);
}
$('#pull-requests-container').arrive('.pull-request-activities, .changes', initOnReady);
$(document).ready(initOnReady);
})();