Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Automatically save marks.
// ============================================================ //
// Inside of the PRESERVE Area
// ============================================================ //
//
// * IMPORTANT
// You have to install _scrollet.ks.js in order to use this script.
//
var myMarkKeys = ["1", "2", "3", "4", "5"];
var autoMarkInterval = 5000;
// Import from _scrollet.ks.js.
function findScrollableWindow()
{
let win;
try
{
win = window.document.commandDispatcher.focusedWindow;
if (win && (win.scrollMaxX > 0 || win.scrollMaxY > 0))
return win;
win = window.content;
if (win.scrollMaxX > 0 || win.scrollMaxY > 0)
return win;
for (let frame in win.frames)
if (frame.scrollMaxX > 0 || frame.scrollMaxY > 0)
return frame;
}
catch (x)
{
win = window.content;
}
return win;
}
// Refer the function getCurrentMode in _scrollet.ks.js.
function isViewMode () {
var ev = { originalTarget : document.commandDispatcher.focusedElement || content.document };
if (!ev.originalTarget.localName)
ev.originalTarget = { localName : "" };
return key.getCurrentMode(ev) == key.modes.VIEW;
}
function getScrolletMarks () {
return plugins.scrollet.getMarksForWin();
}
function getOldestMarkKey () {
var oldestTime = Date.now();
var oldestKey = myMarkKeys[0];
for (var i = 0; i < myMarkKeys.length; i++) {
var key = myMarkKeys[i];
var mark = getScrolletMarks()[key];
if (!mark || !mark.date) {
return key;
}
if (mark.date < oldestTime) {
oldestTime = mark.date;
oldestKey = key;
}
}
return oldestKey;
}
function existsSameMark (aX, aY) {
var win = findScrollableWindow();
var marks = getScrolletMarks();
for (var i = 0; i < myMarkKeys.length; i++) {
var key = myMarkKeys[i];
var mark = marks[key];
if (mark) {
if (mark.scrollX == aX && mark.scrollY == aY) {
return true;
}
}
}
return false;
}
function sortMarks () {
var marks = getScrolletMarks();
var markArray = [];
for (var i = 0; i < myMarkKeys.length; i++) {
var key = myMarkKeys[i];
var mark = marks[key];
if (mark) {
markArray.push(mark);
}
}
markArray.sort(
function (obj, obj2) {
return obj2.date - obj.date;
});
for (i = 0; i < myMarkKeys.length; i++) {
var key = myMarkKeys[i];
marks[key] = markArray[i];
}
return marks;
}
function autoSetMark () {
if (isViewMode()) {
var key = getOldestMarkKey();
var marks = getScrolletMarks();
var mark = marks[key];
var win = findScrollableWindow();
var x = win.scrollX;
var y = win.scrollY;
if (!existsSameMark(x, y)) {
plugins.scrollet.setMark(key);
}
marks = sortMarks();
}
}
if (setMarkTimer) {
clearInterval(setMarkTimer);
}
var setMarkTimer = setInterval(autoSetMark, autoMarkInterval);
// autoSetMark ends here.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment