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

拈花ヽ惹草 提交于 2019-12-02 13:58:23

问题


Need help making a script for Greasemonkey that will help me read forums more efficiently.

Redirect all pages ending in .html:

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

To this printable version URL:

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


(Add -print, just before ending .html.


回答1:


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.



来源:https://stackoverflow.com/questions/12659824/greasemonkey-to-redirect-site-urls-from-html-to-print-html

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