Greasemonkey to redirect site URLs from .html to -print.html?

孤街醉人 提交于 2019-12-02 05:05:33

To do this accounting for possible URL parameters and hash tags:

// ==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);
}

Note that we use the regex version of @include.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!