-
-
Save ksoda/2921d33b226965dffc5aeb1ec0102ac9 to your computer and use it in GitHub Desktop.
scroll original window -> new window will be scrolled synchronously
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//javascript: | |
(() => { | |
const mkDebounce = fn => { | |
let timeoutId; | |
let resolves = []; | |
return function(...args) { | |
return new Promise(resolve => { | |
clearTimeout(timeoutId); | |
timeoutId = setTimeout(() => { | |
timeoutId = null; | |
for (r of resolves) r(fn.apply(this, args)); | |
resolves = []; | |
}, 250); | |
resolves.push(resolve); | |
}); | |
}; | |
}; | |
const state = { | |
sync: true, | |
offset: { | |
X: 0, | |
Y: 0 | |
} | |
}; | |
const sideUrl = window.prompt( | |
"Same origin URL to mirror", | |
window.location.href | |
); | |
if (!sideUrl) { | |
return; | |
} | |
Notification.requestPermission(p => { | |
const msgs = [ | |
"You can change offset", | |
"by calling _updateOffset() in DevTools" | |
]; | |
if (p === "granted") | |
new Notification(msgs[0], { | |
body: msgs[1] | |
}); | |
else console.log(msgs.join(" ")); | |
}); | |
const sideWindow = window.open(sideUrl); | |
function updateOffset() { | |
state.sync = !state.sync; | |
state.offset.X = sideWindow.scrollX - window.scrollX; | |
state.offset.Y = sideWindow.scrollY - window.scrollY; | |
} | |
window._updateOffset = updateOffset; | |
const onScroll = mkDebounce(() => { | |
if (state.sync) { | |
sideWindow.scrollTo( | |
window.scrollX + state.offset.X, | |
window.scrollY + state.offset.Y | |
); | |
} | |
}); | |
document.addEventListener("scroll", onScroll); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment