简体   繁体   English

如何在URL中添加参数,然后使用Greasemonkey重新加载页面?

[英]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. 如果您没有登录,Quora网站会将其大部分内容模糊不清。另一种方法是将参数“?share = 1”添加到其URL中。 I think the steps to doing this via Greasemonkey are: 我认为通过Greasemonkey执行此操作的步骤是:

0/ stash the current URL 0 /存储当前URL

1/ check to see if the parameter is already there. 1 /检查参数是否已经存在。 If it is, break. 如果是,请休息。

2/ If not, add parameter. 2 /如果没有,请添加参数。

3/ reload with updated URL. 3 /使用更新的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. 注意:在“settings”中,我将脚本设置为在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 ) 我知道这种基本方法不适用于其他网站,但只是附加“?share = 1” 应该适用于Quora(参见: 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. 当我访问http://www.quora.com/Animals/What-are-some-mind-blowing-facts-from-the-animal-kingdom时 ,页面不会使用添加的参数重新加载所需的新URL。

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM