Change chrome homepage using batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Boxof
Posts: 3
Joined: 22 Mar 2019 12:55

Change chrome homepage using batch

#1 Post by Boxof » 22 Mar 2019 12:59

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!

siberia-man
Posts: 208
Joined: 26 Dec 2013 09:28
Contact:

Re: Change chrome homepage using batch

#2 Post by siberia-man » 22 Mar 2019 15:40

@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:

Code: Select all

"<CHROME-BIN>\chrome.exe" --user-data-dir="<CHROME-DATA>"
<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.

Boxof
Posts: 3
Joined: 22 Mar 2019 12:55

Re: Change chrome homepage using batch

#3 Post by Boxof » 22 Mar 2019 17:00

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?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Change chrome homepage using batch

#4 Post by aGerman » 23 Mar 2019 10:19

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

Boxof
Posts: 3
Joined: 22 Mar 2019 12:55

Re: Change chrome homepage using batch

#5 Post by Boxof » 24 Mar 2019 11:15

Isn't it possible without any programs or tools?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Change chrome homepage using batch

#6 Post by aGerman » 24 Mar 2019 11:52

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

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Change chrome homepage using batch

#7 Post by penpen » 24 Mar 2019 12:06

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

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Change chrome homepage using batch

#8 Post by aGerman » 24 Mar 2019 12:42

penpen wrote:
24 Mar 2019 12:06
So yes you should be able to do that with a hybrid jScript/batch file...
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

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Change chrome homepage using batch

#9 Post by penpen » 25 Mar 2019 07:10

aGerman wrote:
24 Mar 2019 12:42
Not quite. The global JSON object will be provided by the browser. It's not accessible from the script engine for stand-alone scripts.
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
"emptySampleJsonWorker.js.bat":

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);
}
"jsonSample.json":

Code: Select all

{
	"id": "1",
	"type": "color table",
	"color": [
		{
			color: "red",
			value: "#f00"
		},
		{
			color: "green",
			value: "#0f0"
		},
		{
			color: "blue",
			value: "#00f"
		}
	]
}
Result:

Code: Select all

Z:\>test jsonSample.json
1
blue
penpen

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Change chrome homepage using batch

#10 Post by aGerman » 25 Mar 2019 10:34

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

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Change chrome homepage using batch

#11 Post by penpen » 25 Mar 2019 16:52

Yes, you are right, but something like the following "jsonTest.js.bat" should be sufficient for most json files:

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"
		}
	]
}
);
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

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Change chrome homepage using batch

#12 Post by aGerman » 25 Mar 2019 18:05

That would have been my way

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));
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

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Change chrome homepage using batch

#13 Post by penpen » 28 Mar 2019 17:29

@Boxof, i'm not sure if you noticed that liitle snipplet of text, but we need input from you to proceed:
penpen 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.
penpen

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Change chrome homepage using batch

#14 Post by ShadowThief » 28 Mar 2019 21:25

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.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Change chrome homepage using batch

#15 Post by aGerman » 29 Mar 2019 01:10

ShadowThief wrote:
28 Mar 2019 21:25
Also, mine is over 100,000 characters long, so editing it in batch would prove... tricky.
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.

Steffen

Post Reply