将信息从命令行传递到Chrome扩展 [英] Passing information from command line into Chrome extension

查看:195
本文介绍了将信息从命令行传递到Chrome扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在某些情况下更改我的Chrome扩展程序的行为。我控制Chrome的启动,以便我可以添加任何命令行标志到实现这一点(最好不要改变Chrome的行为)。



我需要这些信息在扩展的后台页面(而不是内容页面)中可用。

我想到的一件事是添加一个环境变量,但显然这是

我试图通过命令行切换到< href =https://code.google.com/p/chromium/codesearch#chromium/src/chrome/common/chrome_switches.cc =nofollow noreferrer> Chromium的代码,并认为我可能会能够使用 test-name



test-name 在扩展中可用?如果没有,是否有另一种方法将信息传递给Chrome扩展?

解决方案

无法将信息直接发送到扩展。



您需要以这种或那种方式使用消息传递功能。


  • 例如,您可以使用 Chrome原生消息与Python脚本进行通信。


  • 或者您可以使用网络信息 a>从网页发布消息(可能是一个file:// URL)。



    from selenium import webdriver

    driver = webdriver.Chrome('/ path / to / chromedriver')
    driver.get('file:///some/page.html')
    driver.execute_script('chrome.runtime.sendMessage( ')
    driver.quit()




    I want to be able to change the behaviour of my Chrome extension in certain cases. I control the launch of Chrome so I can add any command line flag to achieve this (preferably not one that changes the behaviour of Chrome).

    I need this information to be available in the background page of the extension (not in content pages).

    One thing I thought of was adding an environment variable, but apparently this isn't directly available from the extensions context.

    I tried looking through the command line switches in Chromium's code and thought I may be able to use test-name.

    Is the test-name available in the extension? If not is there another way to pass information into a chrome extension?

    解决方案

    There is no way to send information to an extension directly.

    You need to use messaging one way or another.

    • You could use Chrome Native Messaging to communicate with a python script for example.

    • Or you could use web messaging to post a message from a web page (which could be a file:// URL).

      • You could try selenium to execute the script directly.

        from selenium import webdriver
    
        driver = webdriver.Chrome('/path/to/chromedriver')
        driver.get('file:///some/page.html')
        driver.execute_script('chrome.runtime.sendMessage("extensionID", "message");')
        driver.quit()
    

    这篇关于将信息从命令行传递到Chrome扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆