User Tools

Site Tools


scroll-percentage

Display scroll percentage (deprecated: script is included since 2009.11.30)

Description

One useful feature of scrollbars is to display the size of the document you are viewing. The following script sets the scroll_message variable to Top, Bot, All or the percentage of the page you have scrolled just like you get in VIM. If you wish you can use different indicators buy setting the following custom variables.

  • scroll_all_indicator
  • scroll_top_indicator
  • scroll_bottom_indicator

JavaScript

// VIM ruler style scroll message
(function() {
	var run = Uzbl.run;
	var update_message = function() {
		var innerHeight = window.innerHeight;
		var scrollY = window.scrollY;
		var height = document.height;
		var message;

		if (UzblZoom.type === "full") {
			var zoom_level = UzblZoom.level;
			innerHeight = Math.ceil(innerHeight * zoom_level);
			scrollY = Math.ceil(scrollY * zoom_level);
			height -= 1;
		}

		if (! height) {
			message = "";
		}
		else if (height <= innerHeight) {
			message = run("print @scroll_all_indicator") || "All";
		}
		else if (scrollY === 0) {
			message = run("print @scroll_top_indicator") || "Top";
		}
		else if (scrollY + innerHeight >= height) {
			message = run("print @scroll_bottom_indicator") || "Bot";
		}
		else {
			var percentage = Math.round(scrollY / (height - innerHeight) * 100);
			message = percentage + "%";
		}
		run("set scroll_message=" + message);
		run("update_gui")
	};

	self.UzblZoom = {
		get level() {
			return Number(run("print @zoom_level")) || 1;
		},
		set level(level) {
			if (typeof level === "number" && level > 0) {
				run("set zoom_level = " + level);
				update_message();
			}
		},
		get type() {
			return run("print @zoom_type") || "text";
		},
		set type(type) {
			if ((type === "text" || type === "full") && this.type != type) {
				run("toggle_zoom_type");
				run("set zoom_type = " + type);
				update_message();
			}
		},
		toggle_type: function() {
			this.type = (this.type === "text" ? "full" : "text");
		}
	};

	window.addEventListener("DOMContentLoaded", update_message, false);
	window.addEventListener("load", update_message, false);
	window.addEventListener("resize", update_message, false);
	window.addEventListener("scroll", update_message, false);
	update_message();
})();

// vim: set noet ff=unix

Installation

Save the above script to a directory of your choice and add it to load_commit_handler, change your zoom binds and add the variable to your title or status bar eg.

set load_commit_handler = script $XDG_DATA_HOME/uzbl/scripts/scroll_msg.js
set scroll_all_indicator = <span foreground="red">All</span>
set scroll_top_indicator = <span foreground="green">Top</span>
set scroll_bottom_indicator = <span foreground="blue">Bot</span>
set status_format = <span font_family="monospace">\@scroll_message</span>

bind zi = js UzblZoom.level += 0.1
bind zo = js UzblZoom.level -= 0.1
bind zz = js UzblZoom.level = 1
bind zt = js UzblZoom.toggle_type()

Issues

  • One side affect of this script is that the zoom_level will persist between pages (personally I think it's as a feature). If you do not like this behaviour simply change your load_commit_handler to the following.
set load_commit_handler = chain "set zoom_level = 1" "script $XDG_DATA_HOME/uzbl/scripts/scroll_msg.js"
  • Scroll percentage doesn't work in the non-html gmail version.

Note to n30n: Everything works perfectly. I must have messed up the installation last time, but now it just works. You rock.

scroll-percentage.txt · Last modified: 2016/08/31 14:10 (external edit)