Skip to content

Instantly share code, notes, and snippets.

@XP1
Created January 31, 2012 14:47
Show Gist options
  • Save XP1/1710867 to your computer and use it in GitHub Desktop.
Save XP1/1710867 to your computer and use it in GitHub Desktop.
Restore textarea Scrollbar Position: Restores textarea scrollbar position after the mouse is clicked in the textarea. Workaround for Opera 11.6x scrollbar reset bug when the find bar loses focus.
// ==UserScript==
// @name Restore textarea Scrollbar Position
// @version 1.00
// @description Restores textarea scrollbar position after the mouse is clicked in the textarea. Workaround for Opera 11.6x scrollbar reset bug when the find bar loses focus.
// @author XP1 (https://github.com/XP1/)
// @namespace https://gist.github.com/1710867/
// Wikipedia:
// @include http*://wikipedia.org/*&action=*
// @include http*://*.wikipedia.org/*&action=*
// @include http*://wikimedia.org/*&action=*
// @include http*://*.wikimedia.org/*&action=*
// @include http*://wikibooks.org/*&action=*
// @include http*://*.wikibooks.org/*&action=*
// @include http*://wikinews.org/*&action=*
// @include http*://*.wikinews.org/*&action=*
// @include http*://wikiquote.org/*&action=*
// @include http*://*.wikiquote.org/*&action=*
// @include http*://wikisource.org/*&action=*
// @include http*://*.wikisource.org/*&action=*
// @include http*://wikiversity.org/*&action=*
// @include http*://*.wikiversity.org/*&action=*
// @include http*://mediawiki.org/*&action=*
// @include http*://*.mediawiki.org/*&action=*
// @include http*://wiktionary.org/*&action=*
// @include http*://*.wiktionary.org/*&action=*
// ==/UserScript==
/**
* Workaround for Opera 11.6x scrollbar reset bug when the find bar loses focus.
*
* Known issues:
* 1) When the focus transitions from the find bar to the textarea, the intended text cursor, or caret, position is lost once. Have to manually reset the position again after gaining textarea focus.
* 2) When the find bar is closed without losing focus once, scrollbars still reset because cannot detect when that happens. Current workaround requires a mouse event.
*/
/*jslint browser: true, vars: true, white: true, maxerr: 50, indent: 4 */
(function ()
{
"use strict";
function forEach(collection, callback)
{
var i = null;
var length = collection.length;
for (i = 0; i < length; i += 1)
{
callback(collection[i]);
}
}
function TextareaListener()
{
this.previousScrollTop = null;
this.previousScrollLeft = null;
this.previousSelectionStart = null;
this.previousSelectionEnd = null;
}
TextareaListener.prototype.listen = function listen(event)
{
var type = event.type;
var target = event.target;
var scrollTop = target.scrollTop;
var scrollLeft = target.scrollLeft;
var selectionStart = target.selectionStart;
var selectionEnd = target.selectionEnd;
if (type === "mousedown")
{
// Save textarea properties.
this.previousScrollTop = scrollTop;
this.previousScrollLeft = scrollLeft;
this.previousSelectionStart = selectionStart;
this.previousSelectionEnd = selectionEnd;
}
else if (type === "focus")
{
// Restore textarea properties only after a reset occurs.
var isSelected = (this.previousSelectionStart !== this.previousSelectionEnd);
if (scrollTop === 0 && scrollLeft === 0 && isSelected)
{
target.scrollTop = this.previousScrollTop;
target.scrollLeft = this.previousScrollLeft;
}
}
};
function addListeners(element)
{
var textareaListener = new TextareaListener();
var listen = textareaListener.listen;
element.addEventListener("mousedown", listen, false);
element.addEventListener("focus", listen, false);
element.dataset.listening = "true";
}
function initialize()
{
var textareas = document.querySelectorAll("textarea:not([data-listening])");
forEach(textareas, addListeners);
}
window.addEventListener("DOMContentLoaded", initialize, false);
}());
@XP1
Copy link
Author

XP1 commented Feb 9, 2012

Fixed in Opera 11.62 Build 1297:
http://my.opera.com/desktopteam/blog/2012/02/09/another-11-62-snapshot

CORE-43285 Clicking to set cursor position after searching a textarea fails, and scrolls to the top instead

@XP1
Copy link
Author

XP1 commented Mar 29, 2012

Fixed in official Opera 11.62 build 1347:

"Text cursor position lost when clicking to focus on a search match inside a textarea"

http://www.opera.com/docs/changelogs/windows/1162/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment