问题
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