简体   繁体   English

通过Firefox插件在特定/通配符URL上注入JavaScript / CSS

[英]Inject JavaScript/CSS on specific/wildcard urls via Firefox Addon

I've seen similar questions on here, but none really answered my questions... 我在这里看到过类似的问题,但没有人真正回答我的问题...

  1. How to check if the url matches a string. 如何检查网址是否与字符串匹配。 This part would be easy, but there's a catch... I need a wildcard: eg " google.com/search* " where it would trigger no matter what comes after "search". 这部分将很容易,但是有一个陷阱……我需要一个通配符:例如“ google.com/search* ”,无论“搜索”之后发生什么,它都会在哪里触发。

  2. How to then apply that idea to a Firefox add-on WITHOUT using any XUL (it seems the only thing add-on tutorials teach you is how to deal with XUL add-ons ffs). 然后如何在不使用任何XUL的情况下将该想法应用到Firefox附加组件(附加组件教程似乎唯一教给您的是如何处理XUL附加组件ffs)。 All I want to do is inject my personal JS/CSS into these pages without having to deal with anything overly complicated. 我要做的就是将个人JS / CSS注入这些页面,而不必处理任何过于复杂的事情。

Chrome makes this stupidly easy to do via the manifest, but I have very limited knowledge of Firefox addon development (and JavaScript in general, actually). Chrome可以通过清单轻松实现这一愚蠢的操作,但是我对Firefox插件开发(实际上通常是JavaScript)的了解非常有限。 Any links that could help me would be great too; 任何可以帮助我的链接也都很好。 I don't mind reading, I just need to know WHERE to read! 我不在乎阅读,我只需要知道在哪里阅读!

You need String.prototype.indexOf to determine if that string is contained within your search. 您需要String.prototype.indexOf来确定搜索中是否包含该字符串。

if (-1 !== 'google.com/search/oreo') {
    console.log("Jackpot");
}

However this will trigger even when the string is somewhere else in your string, like: 但是,即使字符串在字符串中的其他位置,也会触发此操作,例如:

if (-1 !== 'https://unrlelated.domain.com/blog/google.com/search/oreo') {
    console.log("False alert");
}

For more control, you need regular expressions . 要进行更多控制,您需要使用正则表达式

if (null !== /^http(s?):\/\/(www\.)?google\.com\/search/i) {
    console.log("Yay, it starts with google.com!");
}

For injecting something between you might use String.prototype.replace . 为了在它们之间注入一些东西,可以使用String.prototype.replace As for how, I'll leave it as an exercise 至于如何,我将其保留为练习

As you might've seen I've linked mdn a few times. 如您所见,我已经链接了几次。 It's great resource, all you have to do is to google "mdn keyword" and you'll have almost the best explanation there. 这是一个很好的资源,您所要做的就是使用Google“ mdn关键字”,在那里您几乎可以获得最好的解释。

Are you using Firefox Add-SDK? 您正在使用Firefox Add-SDK吗? Or doing pure bootstrap addon? 还是做纯自举插件?

If you are using add-sdk its incredibly easy as well. 如果您使用add-sdk,它也非常容易。

var contentScriptString = '$("body").html("<h1>Page matches ruleset</h1>");';

pageMod.PageMod({
  include: "*.mozilla.org",
  contentScript: contentScriptString,
  contentScriptFile: data.url("jquery.js")
});

This will match anything that ends with *.mozilla.org 这将匹配以*.mozilla.org结尾的任何*.mozilla.org

So you would do include: "http://www.google.com/search*" 因此,您需要include: "http://www.google.com/search*"

Read all about the dumbed down pattern matching used in add-on sdk which is still pretty powerful: MDN :: Add-on SDK - Modifying Web Pages Based on URL 阅读有关附加sdk中使用的精简模式匹配的所有内容,该功能仍然非常强大: MDN ::附加SDK-基于URL修改网页

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM