• 0

(JS) Help me with a Greasemonkey Userscript


Question

Hey guys. I've noticed a lot of sides are adding keyboard page scrolling via the left and right arrow keys. I often use the up and down to scroll a page and end up hitting the wrong one which opens the next or previous page. I'd like to disable this if possible, I figured a Userscript would be best.

Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0
http://www.thechive.com and http://www.omgubuntu.co.uk/ are the two I use the most

These websites are doing pagination wrong. Pagination should be done with HTML sequential relationships and then fallback using JavaScript when the user presses a modifier key and arrow key. The modifier key that Opera uses is Ctrl. For example, combined with an arrow key, Ctrl + Right arrow navigates to the next page.

http://www.w3.org/TR/html4/struct/links.html#h-12.1.2

http://www.whatwg.org/specs/web-apps/current-work/multipage/links.html#sequential-link-types

Opera is the only major browser that supports keyboard navigation with sequential relationships.

Here is a simple user JS for Opera that disables the left and right arrow key navigation on those websites:

"Disable keyboard navigation 1.00.js":

// ==UserScript==
// @name Disable keyboard navigation
// @version 1.00
// @description Disables left and right arrow key navigation on webpages.
// @namespace https://github.com/XP1/
// @copyright 2013
// @author XP1
// @homepage https://github.com/XP1/
// @license Apache License, Version 2.0; http://www.apache.org/licenses/LICENSE-2.0
// @include http*://omgubuntu.co.uk/*
// @include http*://*.omgubuntu.co.uk/*
// @include http*://thechive.com/*
// @include http*://*.thechive.com/*
// ==/UserScript==

/*
* Copyright 2013 XP1
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*jslint browser: true, vars: true, maxerr: 50, indent: 4 */
(function (window) {
"use strict";

var keyCode = {
leftArrow: 37,
upArrow: 38,
rightArrow: 39,
downArrow: 40
};

function onKeyDown(event) {
var isModifierKeyPressed = (event.shiftKey || event.ctrlKey || event.altKey);
if (isModifierKeyPressed) {
return;
}

var currentKeyCode = event.keyCode;
if (currentKeyCode === keyCode.leftArrow || currentKeyCode === keyCode.rightArrow) {
event.preventDefault();
event.stopPropagation();
}
}

window.addEventListener("keydown", onKeyDown, true);
}(this));[/CODE]

Link to comment
Share on other sites

  • 0

Sweet, that worked. Thank you so much!

Edit: Damn it doesn't work on www.9gag.com any idea why?

Works for me.

Did you add this and force refresh, clear cache, or restart?:

// @include http*://9gag.com/*
// @include http*://*.9gag.com/*

Link to comment
Share on other sites

  • 0

No. I didn't realize I could do that. I don't know JS at all. Now I assume I could add any domain in the same way?

Edit: Also, Greasemonkey is warning me to have a line for "metablock @grant"

http://wiki.greasespot.net/@grant

Yes. You could @include and @exclude sites as well.

I use Opera, so it may not have Greasemonkey-specific features. You could add `@grant none` and see if it works.

Link to comment
Share on other sites

  • 0

That's what I thought, because it looks like that would apply to Greasemonkey's API which I don't think you used? Again, thank you so much!

Link to comment
Share on other sites

  • 0

Hey this script has since stopped working on the site OMG Ubuntu. I'm not sure what's changed but today I noticed it started changing articles on me again.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.