12

我的目标是通过 Python 将 Adblock Plus 与 Selenium 一起使用。我已经能够让它加载扩展,但默认情况下它不包括默认过滤器“EasyList”。这是我到目前为止所拥有的:

 from selenium import webdriver
 from time import sleep
 ffprofile = webdriver.FirefoxProfile()
 adblockfile = '/Users/username/Downloads/adblock_plus-2.4-tb+fx+an+sm.xpi'
 ffprofile.add_extension(adblockfile)
 ffprofile.set_preference("extensions.adblockplus.currentVersion", "2.4")
 browser = webdriver.Firefox(ffprofile)
 while(True):
    browser.get("www.cnn.com")
    sleep(5)

大部分代码都是从http://selenium-python.readthedocs.org/en/latest/faq.html

4

3 回答 3

13

实际上,Adblock Plus 默认会添加 EasyList - 但如果您将extensions.adblockplus.currentVersion首选项设置为禁用更新/首次运行操作,则不会。我猜你的目标是阻止首次运行页面出现,但它也阻止了数据存储初始化。请注意,这里还有更多问题:即使 Adblock Plus 添加了 EasyList,下载仍然需要未知时间。

更好的做法应该是使用现有文件初始化您的配置adblockplus/patterns.ini文件。您可以使用 EasyList 和其他过滤器设置从您的常规 Firefox 配置文件中获取此文件,然后将其复制到/Users/username/Downloads/profilemodel/adblockplus/patterns.ini. 然后以下应该工作:

ffprofile = webdriver.FirefoxProfile("/Users/username/Downloads/profilemodel");
于 2013-12-30T08:20:03.967 回答
2

有一个更好的方法来做到这一点:

1) 使用 7-zip 或等效文件提取 adblock.xpi

2) 使用常规文本编辑器打开 /modules/AppIntegration.jsm

3) 找到“notifyUser()”的函数声明,并将其替换为简单的返回。例如:

/**
* function notifyUser()
* {
*   let wrapper = (wrappers.length ? wrappers[0] : null);
*   if (wrapper && wrapper.addTab)
*   {
*       wrapper.addTab("chrome://adblockplus/content/ui/firstRun.xul");
*   }
*   else
*   {
*       Utils.windowWatcher.openWindow(wrapper ? wrapper.window : null,
*                                                                    "chrome://adblockplus/content/ui/firstRun.xul",
*                                                                    "_blank", "chrome,centerscreen,resizable,dialog=no", null);
*   }
* }
*/

function notifyUser()
{
    return;
}

现在您只需要将文件打包回 zip 文件,并将扩展名从 .zip 更改为 .xpi——瞧!

这将阻止 adblock 加载欢迎页面,但仍会配置必要的订阅设置。确保不要打电话

ffprofile.set_preference("extensions.adblockplus.currentVersion", "x.x.x")

否则,它将不知道“引导自己”

请注意,这是针对 adblock_plus-2.0.3 的,因为我使用的是 firefox-17。代码可能略有不同,并且在新版本的不同位置。请参阅:https ://issues.adblockplus.org/ticket/206#comment:5

于 2014-10-29T16:39:33.407 回答
0

我最近刚刚通过反复试验发现了如何在没有任何问题的情况下阻止首次运行页面:

  1. 转到扩展程序所在的文件夹

  2. lib/adblockplus.js,设置defaults.suppress_first_run_pagetrue而不是false

于 2020-05-29T14:02:07.630 回答