A
A
AlexRAV2016-08-19 19:20:31
JavaScript
AlexRAV, 2016-08-19 19:20:31

How to enable greasemonkey script on 404 page?

Hi all. How to make greasemonkey script work on 404 page? Even just trying to display an alert, it does not work, although if you click on the extension icon, it will show that the script is working on this page

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey A, 2016-08-20
@allewa

If you look for such solutions, then the majority rests on the search for the string "not found" and / or "404".
I would prefer the re-request option.
For a specific site, specify // include http://site.com/*
To process all 404 pages // include *
It is also important to specify// @run-at document-start

The code
// ==UserScript==
// @name        page404handler
// @namespace   page404handler
// @version     0.0.1
// @include     *
// @grant       none
// @run-at      document-start
// ==/UserScript==

  var url = document.location.href;
  var xhr = new XMLHttpRequest();

  xhr.onreadystatechange = function() {
    if (xhr.readyState == 4 && xhr.status === 404) {
      console.log('Page not found');
      // do something
    }
  }
  xhr.open('GET', url, true);
  xhr.send();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question