-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkext-jquery.js
115 lines (102 loc) · 3.94 KB
/
linkext-jquery.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
(function(){
/*
* - https://www.smashingmagazine.com/2010/05/make-your-own-bookmarklets-with-jquery/
* - http://pastie.org/462639
* - http://www.codetoad.com/javascript_get_selected_text.html
*/
function main() {
// the minimum version of jQuery we want
var v = "3.1.0";
// check prior inclusion and version
if (window.jQuery === undefined || window.jQuery.fn.jquery < v) {
var done = false;
var script = document.createElement("script");
script.src = "https://ajax.googleapis.com/ajax/libs/jquery/" + v + "/jquery.min.js";
script.onload = script.onreadystatechange = function(){
if (!done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) {
done = true;
initMyBookmarklet();
}
};
document.getElementsByTagName("head")[0].appendChild(script);
} else {
initMyBookmarklet();
}
}
function getClosestIdAttrNode_brute(origNode) {
if (origNode.id !== "") {
return origNode;
}
var nodes = [];
var allNodes;
if (document.all) {
allNodes = document.all;
} else {
allNodes = document.getElementsByTagName('*');
}
for (var i=0; i < allNodes.length; i++) {
node = allNodes[i];
if (node.id !== "") {
nodes.push(node);
};
if (node == origNode) {
//return false
break;
}
}
var closestIdNode = nodes[nodes.length - 1];
return closestIdNode;
}
function initMyBookmarklet() {
window.jQuery.fn.extend({reverse: Array.prototype.reverse});
window.getClosestIdAttrNode = getClosestIdAttrNode_brute;
console.log('initMyBookmarklet');
(window.myBookmarklet = function() {
// your JavaScript code goes here!
window.linkextEvent = function(event) {
// var text = $(event.target).text();
// var clickedElem = $(event.target);
console.log('event.target')
console.log($(event.target));
console.log('event.target.id');
console.log($(event.target).attr('id'));
$(event.target).fadeOut(50).fadeIn(50).fadeOut(50).fadeIn(50);
var closestElemWithId = window.getClosestIdAttrNode(event.target);
console.log(closestElemWithId);
var closestElemId = closestElemWithId.id;
console.log(closestElemId);
document.location.hash = closestElemId;
var closestElemUrl = document.location.toString();
console.log(closestElemUrl);
var selected_text = getSelText();
var text_and_link = selected_text + "\n-- " + closestElemUrl;
console.log(text_and_link);
window.linkextEvent = "";
$(closestElemWithId).fadeOut(50).fadeIn(50).fadeOut(50).fadeIn(50);
$(document).unbind("click", window.linkextEvent);
}
$(document).click(window.linkextEvent);
})
window.myBookmarklet();
}
function getSelText() {
var SelText = '';
if (window.getSelection) {
SelText = window.getSelection();
} else if (document.getSelection) {
SelText = document.getSelection();
} else if (document.selection) {
SelText = document.selection.createRange().text;
}
return SelText;
}
if (typeof module !== 'undefined') {
module.exports = function(window) {
return {
'getClosestIdAttrNode': getClosestIdAttrNode_brute,
'getClosestIdAttrNode_brute': getClosestIdAttrNode_brute
}
}
}
main();
})(window);