kevmo
kevmo

Reputation: 757

How can I add a parameter to a URL and then reload the page using Greasemonkey?

The website Quora blurs out most of its content if you're not logged in. One way around this is to add the parameter "?share=1" to its URL. I think the steps to doing this via Greasemonkey are:

0/ stash the current URL

1/ check to see if the parameter is already there. If it is, break.

2/ If not, add parameter.

3/ reload with updated URL.

It is similar to this question but it seems to me that this could be done without regex? I could be wrong.

This is the code I'm trying to use:

// ==UserScript==
// @name       Quora Share
// @namespace  kevmo.info
// @version    0.1
// @description  adds "?share=1" to URLS, i.e. let's you view full Quora content w/o being logged in.
// @include      https://*.quora.com/*
// @include      http://*quora.com/*
// @copyright  Creative Commons
// ==/UserScript==

var url = window.location.href;

if (url.indexOf("?share=1") !== -1){
    break;
}
else{
    url +="?share=1";
    window.location.replace(url)
}

Note: in "settings", i set the script to run at document-start.

I know that this sort of basic approach would not work on other websites, but merely appending "?share=1" should work on Quora (see: http://blog.quora.com/Making-Sharing-Better)

When I visit http://www.quora.com/Animals/What-are-some-mind-blowing-facts-from-the-animal-kingdom, the page does not reload with the desired new URL with the added parameter.

Upvotes: 3

Views: 5640

Answers (1)

Chad Seaman
Chad Seaman

Reputation: 36

meta-edit: you're using "break;" while not in a looping structure.

    function share_redirect() {
    var new_url = false;

    if (window.location.hash.length === 0  && window.location.search.lenth === 0) {
         new_url = window.location.href+"?share=1"
    } else {

         if (window.location.search.indexOf('share=1') != -1) {
             return false; // already sharing
         }

         if (window.location.search.length && window.location.hash.length) {

             new_url = window.location.href.split('#')[0]+"&share=1"+window.location.hash;
         } else if (window.location.search.length === 0 && window.location.hash.length) {
             new_url = window.location.href.split('#')[0]+"?share=1"+window.location.hash;
         } else {
             new_url = window.location.href+"&share=1";
         }
    }
    if (new_url) {
        window.location = new_url;
    }
}

Upvotes: 2

Related Questions