Search code examples
javascripthtmlgreasemonkeyuserscriptstampermonkey

Tampermonkey - add page using non-existing URL


I'm creating a userscript which adds new functions to a website. The website has many users, but doesn't have a feature to search for users.
I want to create such a function. To do that, I have created a button in the already existing search page for other search purposes. When I click the button, I need the script to search for the input on Google and fetch the URLs and show the results in a piece of HTML code on a non-existing page.

Can I fake an URL with a userscript, so that it uses it to show HTML?

If not, can I replace certain HTML within the page?

The code isn't really that interesting. It just adds a button with a link and selects it when on the non-existing page.

CODE:

if (document.URL == "http://www.bierdopje.com/search" || document.URL == "http://www.bierdopje.com/search/" || window.location.href.indexOf("search/shows") > -1 || window.location.href.indexOf("search/episodes") > -1 || window.location.href.indexOf("search/forum") > -1) {
var users = document.createElement('li');
users.setAttribute('class', 'strong');
var UsersNode = document.createTextNode("Gebruikers");
var UsersLink = document.createElement('a');
UsersLink.setAttribute('href', 'http://www.bierdopje.com/search/users/');

document.getElementById("submenu").childNodes[1].appendChild(users).appendChild(UsersLink).appendChild(UsersNode);

if (window.location.href.indexOf("search/users/") > -1) {
    UsersLink.setAttribute('href', './');
    UsersLink.setAttribute('class', 'selected');
}
}


Solution

  • Sorry for answering my own question, but like Brock Adams already said: it may have been too localized.
    The solution to fake an url is to replace the 404 not found content.

    If there's like a container with a header and a paragraph, find the container by making it a variable, and then replace it with another variable:

    // find the container
    var example = document.getElementById('container').childNodes[0];
    
    // set new container
    var newcontainer = document.createElement('div');
    newcontainer.setAttribute('id', 'ncontainer');
    
    // replace the existing container with the new one
    example.parentNode.replaceChild(replacement, example);
    
    // write content to the new container
    document.getElementById('ncontainer').innerHTML ='<p>This is not a 404 anymore</p>';
    

    There are probably a lot more and shorter ways to accomplish this, but they can be found by Google (javascript replace).

    To replace the complete page, use

    document.write()
    


    To finish the page, you can set the title with the following:

    document.title = "WEBSITE TITLE";