[ prog / sol / mona ]

prog


javashit redirect script

1 2020-01-08 09:17

What the script is meant to do, redirect to archive.org version of target website, in this case reddit.com, then stop.

What it actually does, endless recursion til timeout.

What I've tried, putting in a break clause, but I think my syntax was wrong.

    if (url.indexOf('reddit.com') !== - 1) {
        location.href='https://web.archive.org/web/*/'+location.href; 
            {break; }
    }
    
2 2020-01-08 14:43

There is no recursion or looping in the snippet you posted.

3 2020-01-08 14:49

>>2

Technically yes. Practically the code isn't valid and doesn't run as:

SyntaxError: unlabeled break must be inside loop or switch

If you take the break out, there's recursion.

Try it yourself.

4 2020-01-08 19:38

Where are you running this script? It works fine on my machine.
Firefox 68.3.0esr (64-bit).

5 2020-01-08 21:01

I'm on FF 72.0.1 (64-bit). Running it as a Greasemonkey script

Here, see for yourself: https://files.catbox.moe/t0c4i8.mp4

The full code is this, not that I think it makes any difference:

// ==UserScript==
// @name        NewsFix
// @description Redirects news articles from The Sun, The Times and The Daily Mail to the internet archive
// @include     *thesun*
// @include     *thetimes*
// @include     *dailymail*
// @include     *reddit*
// @version     1.0
// @require  https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @grant    GM_addStyle
// @run-at document-start
// ==/UserScript==

// Variables for use throughout the script

var url = window.location.toString();

var page = true;

// Redirecting 


while (page){
   if (url.indexOf('reddit.com') != - 1) {
        location.href='https://web.archive.org/web/*/'+location.href;
     page = false;
  } 

}

I tried adding the page variable to break the loop, no luck.

6 2020-01-08 23:04

I think I now why this is happening.

1. Access https://reddit.com;
2. script checks if there's __https://reddits.com__ on your URL;
3. if your condition is true, then your script does __'https://web.archive.org/web/*/' + https://reddit.com__ and send you to this addres;
4. script checks if there's __https://reddits.com__ on your URL;
5. Yeah, there is https://reddit.com on there, you've just added it!
6. then your script does __'https://web.archive.org/web/*/' + https://reddit.com__ and send you to this addres;
7. Loop!

I think you can set the script to run at a specific website.

7 2020-01-08 23:55

>>5
Simply remove the while loop and dump page as well. In your current version page would have worked if you had it inside the while but outside the if -- which would render the while pointless. The reason for your infinite loop is not what >>6 said but the fact that url keeps its original value.

8 2020-01-09 00:16

To clarify, the last bit refers to the version prior to adding page. If Greasemonkey itself is rerunning your script on every new page then change your condition to

(url.indexOf('reddit.com') != - 1) && (url.indexOf('web.archive.org') < 0)
9 2020-01-09 01:22

Looking at the Greasemonkey "Include and exclude rules" at
https://wiki.greasespot.net/Include_and_exclude_rules
it seems your include rules are too lax. Change e.g.

// @include     *reddit*

to

// @include     https://www.reddit.com/*

otherwise your script would apply to all sorts of sites like tiredditties.com or coloreddither.edu.

10 2020-01-09 16:01

Thanks guys.

>>9
Finally it works!

11 2020-01-09 16:17

Ok this is the finished version. Only problem is, the whole purpose of it is to prevent loading pages from the variable ```sites```. And this script seems to load the page first then redirect to archive.org. I think the iteration of the sites array is slowing things down. Can the code be optimized? What if instead of the iteration I just copy pasted the code for each site? Would that quicken things?

var url = window.location.toString();

var sites = ['thesun.co.uk','telegraph.co.uk','dailymail.co.uk','thetimes.co.uk'];

// Redirecting 

for (i=0;i<sites.length;i++) {

   if (url.indexOf(sites[i]) != - 1) {
        location.href='https://web.archive.org/web/*/'+location.href;
    
  } 

}
12 2020-01-09 23:31

>>11
https://wiki.greasespot.net/Metadata_Block#.40run-at
"""@run-at
Compatibility: Limited support in Greasemonkey 4.0.
[...]
document-end
The default if no value is provided. The script will run after the main page is loaded, but before other resources (images, style sheets, etc.) have loaded. The only guaranteed working value in Greasemonkey 4.x."""

So the documentation disclaims any hard guarantees about document-start. You could check whether Violentmonkey or Tampermonkey might work better for your use case. Also:

for (site of sites) {
    ...
    break;
}
13 2020-01-09 23:48

Also:

// @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js

your script can't run before its requirements are loaded, so try removing that jquery line to speed up the loading time.

14 2020-01-10 01:06

So in the 3.x series you can have proper document-start with the patch from
https://github.com/greasemonkey/greasemonkey/issues/1849
but you give up on GM_addStyle. In the current version (4.9) the script invocation lives in
https://github.com/greasemonkey/greasemonkey/blob/4.9/src/bg/execute.js
and proper document_start is in the TODO stage:
"""/*
This file is responsible for observing content navigation events and triggering
content script executions.
TODO: Make document_start execution time work as intended.
*/"""

15


VIP:

do not edit these