问题
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