Change chrome homepage using batch
Moderator: DosItHelp
Change chrome homepage using batch
Hello,
I've been trying to change Google Chrome homepage via batch script and I still cant find an answer. Is it possible?
Also, is there any way to just copy file and add it to autostart folder?
Thanks in advance!
I've been trying to change Google Chrome homepage via batch script and I still cant find an answer. Is it possible?
Also, is there any way to just copy file and add it to autostart folder?
Thanks in advance!
-
- Posts: 208
- Joined: 26 Dec 2013 09:28
- Contact:
Re: Change chrome homepage using batch
@Boxof,
What do you mean saying 'chrome homepage'? Does it mean the home or working directory where the application stores its temporary and persistent files? If your answer is yes, so I can suggest you to use command line options that are supported by chrome:
<CHROME-BIN> and <CHROME-DATA> are the directories where the application itself and its data are living. Of course, you can simply create a shortcut with the real paths and place it somewhere convenient for you.
What do you mean saying 'chrome homepage'? Does it mean the home or working directory where the application stores its temporary and persistent files? If your answer is yes, so I can suggest you to use command line options that are supported by chrome:
Code: Select all
"<CHROME-BIN>\chrome.exe" --user-data-dir="<CHROME-DATA>"
Re: Change chrome homepage using batch
I just need to make a script that changes Google Chrome homepage. Default It's https://google.com. How to create a script that changes this homepage to eg. https://youtube.com?
Re: Change chrome homepage using batch
I don't have Chrome installed anymore. (Edge is good enough.) I remember that Chrome saved it in a file named "preferences" in folder "%localappdata%\Google\Chrome\User Data\Default". It's a JSON file though. Once you found out what object you have to update and how, you should rather use a 3rd party tool which is made for editing JSON data.
Steffen
Steffen
Re: Change chrome homepage using batch
Isn't it possible without any programs or tools?
Re: Change chrome homepage using batch
I don't think so. If I remember right the entire JSON text consists of only one line which will certainly exceed the string limit of 8191 characters in batch. You could try to achieve your goal using JREPL.BAT. But actually RegEx replacement is not an appropriate way to edit JSON text. For sure more reliable would be a tool which is able to parse the JSON markup as a tree of objects in order to find the right nodes.
Why do you even need to change it programmatically? People usually know how to change the related settings in their browser. And just in case you want to change it for other users/computers then my advice is DON'T. You will find these users upset and you will find your script considered as malware.
Steffen
Why do you even need to change it programmatically? People usually know how to change the related settings in their browser. And just in case you want to change it for other users/computers then my advice is DON'T. You will find these users upset and you will find your script considered as malware.
Steffen
Re: Change chrome homepage using batch
Well "json" is short for JavaScript Object Notation, so if that is correct then you probably can do that using JavaScript.
So yes you should be able to do that with a hybrid jScript/batch file...
but you have to find someone with Chrome to do that, or you might post a sample configuration file and find out which data-field has to be changed.
Beside that i agree with aGerman on the "Why do you even need to change it programmatically?" part.
penpen
So yes you should be able to do that with a hybrid jScript/batch file...
but you have to find someone with Chrome to do that, or you might post a sample configuration file and find out which data-field has to be changed.
Beside that i agree with aGerman on the "Why do you even need to change it programmatically?" part.
penpen
Re: Change chrome homepage using batch
Not quite. The global JSON object will be provided by the browser. It's not accessible from the script engine for stand-alone scripts.
You would have to create an HTMLFile object in order to automate the Microsoft HTML rendering engine. I wrote an example for VBScript that could be translated though.
https://www.coding-board.de/resources/v ... ndows.116/
It's in German but the comments in the code are in English.
Steffen
Re: Change chrome homepage using batch
Well you could just copy the json object into a worker jScript - it's just a regular java script object (and ECMA-Script should cover that).
"test.bat":
Code: Select all
@echo off
setlocal enableExtensions disableDelayedExpansion
if "%~1" == "" goto :usage
if not exist "%~1" goto :usage
set "jsonWorker=jsonWorker.js.bat"
> "%jsonWorker%" (
type "emptySampleJsonWorker.js.bat"
echo(
echo(main^(
type "%~1"
echo(^);
)
call "%jsonWorker%"
del "%jsonWorker%"
goto :eof
:usage
echo(Usage: %~nx0 "<json-file>"
goto :eof
Code: Select all
@if (true==false) then /*
@echo off
cscript //nologo //e:JScript "%~f0" %*
exit /b %errorlevel%
*/
@end
function main(jsonSample) {
WScript.echo(jsonSample.id);
WScript.echo(jsonSample.color[2].color);
}
Code: Select all
{
"id": "1",
"type": "color table",
"color": [
{
color: "red",
value: "#f00"
},
{
color: "green",
value: "#0f0"
},
{
color: "blue",
value: "#00f"
}
]
}
Code: Select all
Z:\>test jsonSample.json
1
blue
Re: Change chrome homepage using batch
That's interesting. I didn't expect that because the difference between JSON markup and the script notation of JavaScript objects is that names are quoted in JSON. However, that's just the workaround for the JSON.parse() method. You still have to implement the JSON.stringify() method to write it back. Am I right?
Steffen
Steffen
Re: Change chrome homepage using batch
Yes, you are right, but something like the following "jsonTest.js.bat" should be sufficient for most json files:
Note that the array is stored in a different way than given - so if the above were sufficient depends on how Chrome stores such things.
Theoretically both representations should be equivalent, but the output-form seems to be more standard according to http://json.org/.
penpen
Code: Select all
@if (true==false) then /*
@echo off
cscript //nologo //e:JScript "%~f0" %*
exit /b %errorlevel%
*/
@end
function traverse(key) {
var type = ({}).toString.call(key).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
switch(type) {
case "boolean":
case "number":
case "regexp":
WScript.StdOut.Write("" + key);
break;
case "string":
WScript.StdOut.Write("\"" + key + "\"");
break;
case "array":
var first = true;
WScript.StdOut.Write("[");
for (var name in key) {
if (!first) WScript.StdOut.WriteLine(",");
traverse(key[name]);
first = false;
}
WScript.StdOut.Write("]");
break;
case "object":
if (key === null) {
WScript.StdOut.Write("" + key);
} else {
var first = true;
WScript.StdOut.Write("{");
for (var name in key) {
if (!first) WScript.StdOut.Write(",");
traverse(name);
WScript.StdOut.Write(":");
traverse(key[name]);
first = false;
}
WScript.StdOut.Write("}");
}
break;
default:
WScript.StdOut.WriteLine("unknown type:" + type + ", key:" + key);
break;
}
}
function main(jsonSample) {
WScript.echo(traverse(jsonSample));
}
main(
{
"test_0": null,
"test_1": 0,
"test_2": "1",
"test_3": /a-z/,
"test_4": true,
"test_5": NaN,
"test_6": [],
"color": [
{
color: "red",
value: "#f00"
},
{
color: "green",
value: "#0f0"
},
{
color: "blue",
value: "#00f"
}
]
}
);
Theoretically both representations should be equivalent, but the output-form seems to be more standard according to http://json.org/.
penpen
Re: Change chrome homepage using batch
That would have been my way
But your method might be better on a long term. The IE is about to die and thus, its automation objects will die, too.
Steffen
Code: Select all
@if (@a)==(@b) @end /*
@echo off
cscript //nologo //e:jscript "%~f0" "jsonSample.json"
pause
goto :eof */
var objHTML = new ActiveXObject('HTMLFile'),
objFSO = new ActiveXObject('Scripting.FileSystemObject');
objHTML.open();
objHTML.write('<html><head><title></title><meta http-equiv="x-ua-compatible" content="IE=9" /></head></html>');
objHTML.close();
var JSON = objHTML.parentWindow.JSON;
var fileStream = objFSO.OpenTextFile(WScript.Arguments(0));
jsonObj = JSON.parse(fileStream.ReadAll());
fileStream.Close();
// Change the jsonObj here ...
// Write it back in one line (2nd and 3rd argument of stringify omitted) ...
fileStream = objFSO.OpenTextFile(WScript.Arguments(0), 2);
fileStream.Write(JSON.stringify(jsonObj));
fileStream.Close();
// ... or even pretty print (indentation of 4 spaces)
WScript.Echo(JSON.stringify(jsonObj, null, 4));
Steffen
Re: Change chrome homepage using batch
@Boxof, i'm not sure if you noticed that liitle snipplet of text, but we need input from you to proceed:
penpenpenpen wrote:but you have to find someone with Chrome to do that, or you might post a sample configuration file and find out which data-field has to be changed.
-
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: Change chrome homepage using batch
Took me about five seconds in Google: the homepage setting is in "%LOCALAPPDATA%\Google\Chrome\User Data\Default\Secure Preferences" and the relevant part of the JSON looks like
"homepage":"http://www.google.com/"
Also, mine is over 100,000 characters long, so editing it in batch would prove... tricky.
"homepage":"http://www.google.com/"
Also, mine is over 100,000 characters long, so editing it in batch would prove... tricky.
Re: Change chrome homepage using batch
That's what I was afraid. And that's the reason why have to know the path in the JSON object tree. As I said, I don't have Chrome installed anymore and hence I can't figure it out.ShadowThief wrote: ↑28 Mar 2019 21:25Also, mine is over 100,000 characters long, so editing it in batch would prove... tricky.
Steffen