Created
January 31, 2012 14:47
-
-
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.
This file contains hidden or 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
// ==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); | |
}()); |
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
Fixed in official Opera 11.62 build 1347:
"Text cursor position lost when clicking to focus on a search match inside a textarea"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I posted this user JS in this thread:
New bug in 11.60: Search in text areas is now useless. And some older bugs are not fixed.:
http://my.opera.com/community/forums/topic.dml?id=1190902
Other duplicate threads:
Searching in long form fields, resets to top when clicked:
http://my.opera.com/community/forums/topic.dml?id=1199702
[11.60] [12.0] [bug] Broken search and click in textbox:
http://my.opera.com/community/forums/topic.dml?id=1204192
Wrong cursor after quick search in textarea — new bug (regression) in 11.60:
http://my.opera.com/community/forums/topic.dml?id=1218282