Essentially what I'm trying to do is replace a few values in the JSON string with some variables that I have. Here's a snapshot of my code:
listproxies = ['proxy1', 'proxy2', 'proxy3']
PROXY = random.choice(listproxies)
proxy_host = PROXY[:-28]
proxy_port = '12345'
proxy_username = PROXY[25:-9]
proxy_password = 'password'
print("USING PROXY: "+proxy_host+":"+proxy_port+":"+proxy_username+":"+proxy_password)
manifest_json = """
{
"version": "1.0.0",
"manifest_version": 2,
"name": "Chrome Proxy",
"permissions": [
"proxy",
"tabs",
"unlimitedStorage",
"storage",
"<all_urls>",
"webRequest",
"webRequestBlocking"
],
"background": {
"scripts": ["background.js"]
},
"minimum_chrome_version":"22.0.0"
}
"""
background_js = """
var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "http",
host: """+proxy_host+""",
port: parseInt("""+proxy_port+""")
},
bypassList: ["foobar.com"]
}
};
chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
function callbackFn(details) {
return {
authCredentials: {
username: """+proxy_username+""",
password: """+proxy_password+"""
}
};
}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: ["<all_urls>"]},
['blocking']
);
"""
As you can see, in background_js
I'm trying to call the variables proxy_host
and proxy_port
, and then proxy_username
& proxy_password
further down, which doesn't seem to be working...
JSON格式仅允许使用双引号: ""
使用单引号''
或""" """
不允许。
Use the builtin json
library, and deal with this stuff as if it was a dictionary instead.
>> import json
>> manifest_as_json = json.loads(manifest_json)
>> manifest_as_json['version'] = 1.2.3
>>
>> import pprint
>> pprint.pprint(manifest_as_json)
{
"version": "1.2.3",
"manifest_version": 2,
"name": "Chrome Proxy",
"permissions": [
"proxy",
"tabs",
"unlimitedStorage",
"storage",
"<all_urls>",
"webRequest",
"webRequestBlocking"
],
"background": {
"scripts": ["background.js"]
},
"minimum_chrome_version":"22.0.0"
}
Additionally, if you'd like to, you could use the fstrings in Python and just embed variables into the string:
>> pretty_proxy_host = f"This is my {proxy_host}"
>> print(pretty_proxy_host)
This is my proxy1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.