【问题标题】:Greasemonkey to redirect site URLs from .html to -print.html?Greasemonkey 将站点 URL 从 .html 重定向到 -print.html?
【发布时间】:2012-09-30 08:26:52
【问题描述】:

需要帮助为 Greasemonkey 编写脚本,以帮助我更有效地阅读论坛。

重定向所有以.html结尾的页面:

http://www.site.com/thread-category/4525-url.html

到这个可打印版本的网址:

http://www.site.com/thread-category/4525-url-print.html


(添加-print,就在结束.html之前。

【问题讨论】:

    标签: javascript redirect greasemonkey


    【解决方案1】:

    为此考虑可能的 URL 参数和哈希标签:

    // ==UserScript==
    // @name     _Redirect site.com to print.html URL's
    // @include  /site\.com\/thread.+?\.html\b/
    // @grant    none   
    // @run-at   document-start
    // ==/UserScript==
    
    if ( ! /print\.html$/i.test (location.pathname) ) {
        var printPath   = location.pathname.replace (/(\.html)$/, "-print$1");
        var newURL      = location.protocol + "//"
                        + location.host
                        + printPath
                        + location.search
                        + location.hash
                        ;
        location.replace (newURL);
    }
    

    请注意,我们使用 @include 的正则表达式版本。

    【讨论】:

    • 我建议location.host 超过location.hostname,因为location.host 包括端口(如果存在)。 hostname 从不包含端口号。
    猜你喜欢
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 2021-06-19
    • 2017-07-25
    • 1970-01-01
    • 2012-12-06
    • 1970-01-01
    • 2013-10-25
    相关资源
    最近更新 更多