多个 Greasemonkey 元区块

发布于 2024-09-10 06:18:32 字数 385 浏览 2 评论 0原文

我正在尝试为网站层次结构编写一个 Greasemonkey 脚本,以便对 http: 进行大量代码修改: //www.foo.com/*,然后是 http://www 的更具体内容.foo.com/bar/*,还有 http://www.foo .com/foobar/*

无论如何,我是否可以在同一个脚本中编写所有这些内容,或者我是否必须制作多个?

I'm trying to write a Greasemonkey script for a hierarchy of websites such that I have a bunch of code modifications for http://www.foo.com/*, then more specific ones for http://www.foo.com/bar/*, and still others for http://www.foo.com/foobar/*.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看轻我的陪伴 2024-09-17 06:18:33

有没有办法让我全部写下来
这些在同一个脚本中,或者我有
制作多个?

是的,只需使用这三个 @include,然后在您的用户脚本中执行类似的操作(取决于脚本的具体情况):

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
}

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
}
烟织青萝梦 2024-09-17 06:18:33

有人向我展示了在不同子位置处理不同函数的一个巧妙技巧,那就是使用函数名称的全局目录作为一种虚拟交换机......

// 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.
}

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.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文