Multiple Greasemonkey Metablocks

杀马特。学长 韩版系。学妹 提交于 2019-12-01 12:46:11

Is there anyway for me to write all these in the same script, or do I have to make multiple?

Yes, just use those three @includes, then in your user script do something like (depends on specifics of script):

var currentURL = (document.location+'');
if (currentURL .match(/http:\/\/www\.foo\.com\/foobar\/.*/)) {
  // do stuff for page set A
} else if (currentURL .match(/http:\/\/www\.foo\.com\/foo\/.*/)) {
  // do stuff for page set B
} else if (currentURL .match(/http:\/\/www\.foo\.com\/.*/)) {
  // do stuff for page set C
}

One nifty trick I was shown for dealing with different functions at different sub-locations is to use the global directory of function names as a sort of virtual switchboard...

// do anything that is supposed to apply to the entire website above here.
var place = location.pathname.replace(/\/|\.(php|html)$/gi, "").toLowerCase();
// the regex converts from "foo/" or "foo.php" or "foo.html" to just "foo".
var handler;
if ((handler = global["at_" + place])) {
    handler();
}

// end of top-level code.  Following is all function definitions:
function at_foo() {
    // do foo-based stuff here
}
function at_foobar() {
    // do foobar stuff here.
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!