Repository navigation
Expand file tree
/
Copy pathcontent_script.js
More file actions
52 lines (44 loc) · 1.61 KB
/
Copy pathcontent_script.js
File metadata and controls
52 lines (44 loc) · 1.61 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
window.scroll2message = {
last_clicked_id: null,
sticky_id: null,
setLastClickedId: function (element) {
// bubble upwards and try to find an element that contains an ID
var target = element;
while (!target.id && target.parentElement) {
target = target.parentElement;
}
this.last_clicked_id = target.id ? target.id : null;
},
setStickyId: function () {
this.sticky_id = this.last_clicked_id;
console.log("scroll ID set to", this.sticky_id);
// add the data-scroll attribute to smooth scroll to this element
document.getElementById(this.sticky_id).setAttribute("data-scroll", true);
},
scrollToTarget: function() {
if (this.sticky_id) {
// location.hash = "#" + this.sticky_id;
document.getElementById(this.sticky_id).scrollIntoView();
}
}
};
document.addEventListener("contextmenu", function (event) {
var targetElement = event.target || event.srcElement;
window.scroll2message.setLastClickedId(targetElement);
return false;
});
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
if (request.eventType == "setMessageToScrollTo") {
window.scroll2message.setStickyId();
} else if (request.eventType == "scrollToMessage") {
if (window.scroll2message.sticky_id) {
window.scroll2message.scrollToTarget();
sendResponse({result: "scrolling"});
} else {
sendResponse({result: "targetNotSet"});
}
}
}
);
// smoothScroll.init();