【问题标题】:chrome extension onUpdated fired too latechrome 扩展 onUpdated 为时已晚
【发布时间】:2018-05-06 17:28:08
【问题描述】:

早上好 我尝试使用 chrome 扩展程序或用户脚本在执行之前删除脚本。 我试过了

chrome.tabs.onUpdated.addListener(function(tabid, info, tab) {
    chrome.tabs.executeScript(tabid, {file:"test.js"});
    //if (info.status == "complete") {
      //
   // }
});

在 test.js 中,我删除了第一个脚本元素(仅用于示例)

document.body.querySelectorAll('*')[0].remove()

即使第一次调用 test.js 也为时已晚,页面内的脚本已经执行完毕。 有没有办法在解释之前替换 HTML 或在执行之前删除脚本?

我也为用户脚本尝试了"run_at": "document_start",但脚本是在我要删除的脚本加载到 dom 之前运行的,所以我无法删除它。 过去,使用 beforeload 事件是可能的,但它已从 chrome 中删除。 我也尝试了chrome.runtime.onMessage.addListenerchrome.webRequest.onBeforeRequest.addListener,但是当我重新加载页面时它们不会被触发。 谢谢

【问题讨论】:

  • 那无济于事。您必须重写整个页面 html,example。当然,您还需要在 executeScript 中使用 runAt: 'document_start'。
  • 谢谢。这种方法的问题是请求被发出了两次,但如果我可以先阻止第一个请求,那就是解决方案。
  • 不幸的是,在 Chrome 中这是唯一的方法。
  • window.stop() 是不够的,我已将其替换为 document.close();window.stop();即使我们浪费了一点时间,它也能正常工作。

标签: javascript google-chrome google-chrome-extension


【解决方案1】:

最后,我使用 mitmproxy 代替扩展,它是一个允许修改内容的代理。

安装mitmproxy:

sudo apt install pip
pip3 install beautifulsoup4
#pip3 uninstall mitmproxy #uncomment if already installed
sudo pip3 install mitmproxy==3.0.4

sudo sysctl -w net.ipv4.ip_forward=1 #maybe facultative

创建一个名为 insertJs.py 的 python 修饰符脚本: 以下脚本将在包含 tfarjo 的站点上将 $('html, body').animate({".encode('utf-8') 替换为 ;[].push({".encode('utf-8')

def response(flow):
    if 'tfarjo' in flow.request.url: #here filter url that you want modify
        #here make the replacement you want
        flow.response.content = flow.response.content.replace(
            "$('html, body').animate({".encode('utf-8'),
            ";[].push({".encode('utf-8')
        )

def read_file(filename):
    with open(filename) as f:
        return f.read()

使用修改脚本启动 mitmproxy:

sudo mitmdump -p 8081 -s "insertJs.py"

kill chrome: file -> 退出或“killall chrome”两次

现在我们必须配置代理,创建一个添加 mitmproxy 的 chrome 快捷方式(在 linux mint 上它是在桌面上右键 clic 并创建新的启动器):

/usr/bin/google-chrome-stable %U --proxy-server=127.0.0.1:8081

尝试使用 http 页面而不是 https,如果可行,则配置 https:

对于镀铬 做同样的事情并安装 pem (可能是兼性的) 然后按照这些说明https://superuser.com/questions/1083766/how-do-i-deal-with-neterr-cert-authority-invalid-in-chrome#answer-1083768

(对于firefox,只需单击您的操作系统并配置https即可)

【讨论】:

    猜你喜欢
    • 2017-03-20
    • 1970-01-01
    • 2022-10-24
    • 2018-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    • 1970-01-01
    相关资源
    最近更新 更多