Passing information from command line into Chrome extension

和自甴很熟 提交于 2019-12-10 10:54:16

问题


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?


回答1:


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()
  • Another extension could take care of the messaging for you.


来源:https://stackoverflow.com/questions/34629312/passing-information-from-command-line-into-chrome-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!