Jump to content



Photo

(JS) Help me with a Greasemonkey Userscript


  • Please log in to reply
10 replies to this topic

#1 Kreuger

Kreuger

    Neowin's Local Grouch

  • Joined: 29-December 03
  • Location: Ontario, Canada

Posted 11 April 2013 - 16:57

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.


#2 +Majesticmerc

Majesticmerc

    Resident Idealist

  • Tech Issues Solved: 8
  • Joined: 24-August 05
  • Location: United Kingdom
  • OS: Arch Linux / Win 7
  • Phone: HTC One X

Posted 11 April 2013 - 22:44

Got an example site?

#3 Detection

Detection

    Detecting stuff...

  • Joined: 30-October 10
  • Location: UK
  • OS: 7 SP1 x64

Posted 11 April 2013 - 23:32

Make one that blocks all threads about bieber too will you ;)

#4 OP Kreuger

Kreuger

    Neowin's Local Grouch

  • Joined: 29-December 03
  • Location: Ontario, Canada

Posted 12 April 2013 - 12:37

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

#5 XP1

XP1

    XP1

  • Joined: 03-August 05

Posted 12 April 2013 - 20:06

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...s.html#h-12.1.2
http://www.whatwg.or...tial-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*<img src='http://www.neowin.net/forum/public/style_emoticons/<#EMO_DIR#>/hmm.gif' class='bbc_emoticon' alt=':/' />/omgubuntu.co.uk/*
// @include http*<img src='http://www.neowin.net/forum/public/style_emoticons/<#EMO_DIR#>/hmm.gif' class='bbc_emoticon' alt=':/' />/*.omgubuntu.co.uk/*
// @include http*<img src='http://www.neowin.net/forum/public/style_emoticons/<#EMO_DIR#>/hmm.gif' class='bbc_emoticon' alt=':/' />/thechive.com/*
// @include http*<img src='http://www.neowin.net/forum/public/style_emoticons/<#EMO_DIR#>/hmm.gif' class='bbc_emoticon' alt=':/' />/*.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));


#6 OP Kreuger

Kreuger

    Neowin's Local Grouch

  • Joined: 29-December 03
  • Location: Ontario, Canada

Posted 14 April 2013 - 04:45

Sweet, that worked. Thank you so much!

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

#7 XP1

XP1

    XP1

  • Joined: 03-August 05

Posted 14 April 2013 - 17:50

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*<img src='http://www.neowin.net/forum/public/style_emoticons/<#EMO_DIR#>/hmm.gif' class='bbc_emoticon' alt=':/' />/9gag.com/*
// @include http*<img src='http://www.neowin.net/forum/public/style_emoticons/<#EMO_DIR#>/hmm.gif' class='bbc_emoticon' alt=':/' />/*.9gag.com/*


#8 OP Kreuger

Kreuger

    Neowin's Local Grouch

  • Joined: 29-December 03
  • Location: Ontario, Canada

Posted 14 April 2013 - 17:57

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

#9 XP1

XP1

    XP1

  • Joined: 03-August 05

Posted 14 April 2013 - 18:02

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.

#10 OP Kreuger

Kreuger

    Neowin's Local Grouch

  • Joined: 29-December 03
  • Location: Ontario, Canada

Posted 14 April 2013 - 18:06

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!

#11 OP Kreuger

Kreuger

    Neowin's Local Grouch

  • Joined: 29-December 03
  • Location: Ontario, Canada

Posted 02 September 2013 - 16:12

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.