Greasemonkey将站点URL从.html重定向到-print.html?

时间:2012-09-30 08:26:53

标签: javascript redirect greasemonkey

需要帮助制作Greasemonkey的脚本,这将有助于我更有效地阅读论坛。

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

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

到此可打印版本的网址:

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


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

1 个答案:

答案 0 :(得分:2)

要对可能的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的正则表达式版本。