如何从像vb脚本或.net这样的脚本更改Firefox的代理设置?如果有人能告诉我如何做到这一点,那就太好了。
答案 0 :(得分:0)
您可以通过写入prefs.js
文件来执行此操作,但是,我的理解是此文件仅在启动时加载,并且Firefox可能会在关闭时覆盖您的更改,因此我认为这是唯一安全的方法这将是:
prefs.js
。此代码项目文章应该向您展示如何从vbscript关闭进程:DestroyWindow in VBScript
您可以使用FileSystemObject来读取/写入文件。
我认为这样的事情可以再次启动它:
Set shell = CreateObject("WScript.Shell")
shell.Run Chr(34) & "PathToFireFox.exe" & Chr(34), 1, false
答案 1 :(得分:0)
/*This program will find the path to application data of user,and get the path
(\Mozilla\Firefox\Profiles\*.default) and find proxy from the file prefs.js */
#include <iostream>
#include <sstream>
#include <fstream>
#include <windows.h>
#include <string>
#include <shlobj.h>
#include <winerror.h>
#include <vector>
#define MAX 8192
using namespace std;
wstring ReadProxyFromFile(wstring fileName);
wstring ParseAndGetProxyDetails (wstring strLine, wstring field);
wstring ReadINIFileAndGetFirefoxProfilePath( wstring &profileFilePath );
///
/// This function converts wstring to string.
///
string ToString(wchar_t const * pwch, UINT code_page)
{
// Allocate the appropriate space
int sizeRequired = WideCharToMultiByte( code_page, 0, pwch, -1, NULL, 0, NULL, NULL);
char * ach = new char[sizeRequired + 1];
// Convert it
WideCharToMultiByte( code_page, 0, pwch, -1, ach, sizeRequired, NULL, NULL);
// Put it into the return value
string ret(ach);
// Clean up
delete[] ach;
// Return the new string
return ret;
}
///
/// This function splits a string into fields using a separator.
///
vector<string> Split(string const & str_, char const * sep)
{
string str = str_.c_str();
size_t sep_len = ::strlen(sep);
vector<string> ret;
for (size_t i = str.find(sep); i != str.npos; i = str.find(sep))
{
ret.push_back(str.substr(0, i));
str = str.substr(i + sep_len, str.npos);
}
if (str.length() > 0)
ret.push_back(str);
return ret;
}
///
/// This function converts string to wstring.
///
wstring ToWStr(string const& x, UINT code_page )
{
// Allocate the appropriate space
int sizeRequired = MultiByteToWideChar(code_page, 0, x.c_str(), -1, NULL, 0);
wchar_t * ach = new wchar_t[sizeRequired + 1];
// Convert it
MultiByteToWideChar(code_page, 0, x.c_str(), -1, ach, sizeRequired);
// Put it into the return value
wstring ret(ach);
// Clean up
delete[] ach;
// Return the new string
return ret;
}
int main()
{
LPWSTR wszPath = NULL;
HRESULT hr;
wstring strPath;
hr = SHGetKnownFolderPath(FOLDERID_RoamingAppData , KF_FLAG_CREATE, NULL, &wszPath);
if (SUCCEEDED(hr))
{
strPath = wszPath;
strPath += L"\\Mozilla\\Firefox";
wstring profileFilePath = strPath + L"\\profiles.ini";
wstring strCurrentProfile = ReadINIFileAndGetFirefoxProfilePath(profileFilePath);
wstring prefsPath = strPath + L"\\" + strCurrentProfile + L"\\prefs.js";
wstring strProxy = ReadProxyFromFile(prefsPath);
if (strProxy.length()>0)
{
wcout << "Proxy is " <<strProxy <<endl;
}
return 0;
}
}
wstring ReadINIFileAndGetFirefoxProfilePath( wstring &profileFilePath )
{
// reading each section from ini file
LPWSTR pSecNames= new WCHAR[1024];
int nSectionNum = 1;
wstring strAllSectionNames;
DWORD retVal = GetPrivateProfileSectionNames(pSecNames,1024,profileFilePath.c_str());
if (retVal)
{
for (unsigned int i =0; i < retVal; i++)
{
if (pSecNames[i] == NULL)
{
strAllSectionNames += L"_";
}
else
{
strAllSectionNames += pSecNames[i];
}
}
delete pSecNames;
string sectionNames = ToString(strAllSectionNames.c_str(),CP_UTF8);
vector <string> sectionNamesVector = Split(sectionNames.c_str(),"_");
wstring strSectionName;
for ( int it = 0 ; it < sectionNamesVector.size(); it++ )
{
strSectionName = ToWStr(sectionNamesVector.at(it),CP_UTF8);
wcout << nSectionNum << ". Section name: " << strSectionName;
LPWSTR pProfile= new WCHAR[1024];
DWORD active = GetPrivateProfileString(strSectionName.c_str(),L"Default",NULL,pProfile,MAX,profileFilePath.c_str());
if(active)
{
GetPrivateProfileString(strSectionName.c_str(),L"Path",NULL,(LPWSTR)pProfile,MAX,profileFilePath.c_str());
wstring strPath = pProfile;
delete pProfile;
return strPath;
}
delete pProfile;
nSectionNum++;
}
}
}
wstring ReadProxyFromFile(wstring fileName)
{
wstring proxyType;
wstring proxyHttp;
wstring httpPort;
wstring noProxiesOn;
wstring autoconfigUrl;
wstring proxySettings;
wstring field;
int nProxyType;
wifstream myfile;
wstring strLine;
size_t pos;
myfile.open (fileName.c_str());
if (myfile.is_open())
{
while (!myfile.eof())
{
getline(myfile,strLine);
if (!strLine.find(L"user_pref"))
{
pos = strLine.find_first_of(L", ");
field = strLine.substr(11,pos-12);
if (field == L"network.proxy.type")
{
proxyType = ParseAndGetProxyDetails(strLine, field);
nProxyType = _wtoi(proxyType.c_str());
}
if (field == L"network.proxy.http")
{
proxyHttp = ParseAndGetProxyDetails(strLine, field);
}
if (field == L"network.proxy.http_port")
{
httpPort = ParseAndGetProxyDetails(strLine, field);
}
if (field == L"network.proxy.autoconfig_url")
{
autoconfigUrl = ParseAndGetProxyDetails(strLine, field);
}
if (field == L"network.proxy.no_proxies_on")
{
noProxiesOn = ParseAndGetProxyDetails(strLine, field);
}
}
}
}
myfile.close();
switch (nProxyType)
{
case 0: // no proxy
proxySettings = L"FF:";
break;
case 1: // manual proxy
proxySettings = L"FF:PXY:"+proxyHttp+L":"+httpPort;
break;
case 2: //autoconfig_url
proxySettings = L"FF:URL:"+autoconfigUrl;
break;
case 4: // auto proxy
proxySettings = L"FF:";
break;
default: // system proxy
proxySettings = L"FF::";
break;
}
return proxySettings;
}
wstring ParseAndGetProxyDetails (wstring strLine, wstring field)
{
size_t position;
wstring proxyDetail;
position = strLine.find(field);
if (position != string::npos)
{
proxyDetail = strLine.substr(position);
position = proxyDetail.find_first_of(L",");
proxyDetail = proxyDetail.substr(position+1);
position = proxyDetail.find_first_of(L");");
proxyDetail = proxyDetail.substr(0,position);
}
return proxyDetail;
}