modify home page in js file [solved]

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

modify home page in js file [solved]

#1 Post by abc0502 » 21 Feb 2012 10:38

Hi, i'm bulding a batch part of it will modify the mozilla firefox home page to google so here is the code:
this code will give me the profile name and set that name to %g%


Code: Select all

FOR /F "tokens=2 delims=/" %%g IN ('find /i "path=Profiles/" "%appdata%\mozilla\firefox\profiles.ini"') DO COPY "%a%\prefs.js" "%f%\%%g" /v /y

then
I will use this code:

Code: Select all

FOR /F "tokens=2 delims="" %%h IN ('find /i "%i%" "%k%"') DO ????? /v /y

to find the line that contain the home page modification and replace the home page but this is the problem how i can change the defualt page with http://www.google.com

NOTES:
%i% = user_pref("browser.startup.homepage", "
%h% = the prefs.js path
Last edited by abc0502 on 12 Mar 2012 00:28, edited 1 time in total.

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

Re: modify home page in js file

#2 Post by aGerman » 21 Feb 2012 14:37

Try something like that:

Code: Select all

@echo off &setlocal

:: replace with yours:
set "prefsIn=%AppData%\Mozilla\Firefox\Profiles\287p4xrd.default\prefs.js"
set "prefsOut=new.js"
set "subStr=user_pref("browser.startup.homepage","
set "subStrLen=37"
set "replc=user_pref("browser.startup.homepage", "http://www.google.com/");"

setlocal EnableDelayedExpansion
<"!prefsIn!" >"!prefsOut!" (
  for /f %%i in ('type "!prefsIn!"^|find /c /v ""') do for /l %%j in (1 1 %%i) do (
    set "ln=" &set /p "ln="
    if "!ln:~0,%subStrLen%!"=="!subStr!" (
      echo(!replc!
    ) else (
      echo(!ln!
    )
  )
)

Note that the line will probably not exist if the user didn't define a homepage. Also there are more settings. What happens if the user defined not to use the homepage ...
Further more a user could have more than one profile etc. You should do comprehensive tests ...

Regards
aGerman

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: modify home page in js file

#3 Post by abc0502 » 21 Feb 2012 15:17

Thanks alot "aGerman" I really appreciate the help,
i will try but what i saw in the code you using line numbers my line numbers for that line is 40
and =another computer is 38, i will try to find another way if that didn't work
thanks again i really appreciate your help :D

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

Re: modify home page in js file

#4 Post by aGerman » 21 Feb 2012 15:25

I didn't use a line number. 37 is the length (number of characters) of string user_pref("browser.startup.homepage",
I hoped the variable name would explain it.

Regards
aGerman

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: modify home page in js file

#5 Post by abc0502 » 21 Feb 2012 16:43

sorry my bad i understood that wrong, i found another way now and it is very easy,
it's explaind in the mozilla website (the guide) it is here for any one need it:
https://developer.mozilla.org/En/A_Brief_Guide_to_Mozilla_Preferences#Modifying_preferences

all what i have to do is to make a file user.js and put it in the profile folder the content of the file is at the page
up in the url

and as you said many computers couldn't find the line and people might change the homepage and
the case of many profile this solution fix all of this and here is the code i will use

Code: Select all

rem TO get the profile name
FOR /F "tokens=2 delims=/" %%g IN ('find /i "path=Profiles/" "%appdata%\mozilla\firefox\profiles.ini"') DO set a=%%g /v /y
rem Copy the user.js to profile folder
copy "%x%" "%appdata%\mozilla\firefox\Profiles\%a%"


Note: %x% will be the directory of the pre-made user.js file

but i have a question the case you said mutli profile how i set variables to more than one profile in the file profile.ini
thnx again :D

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: modify home page in js file

#6 Post by abc0502 » 21 Feb 2012 17:22

it seems that there is a wrong thing is this user.js it doesn't work even after changing it to modify the home page there is a override mstone that check the last home page and return it to the previous hoem page :(

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: modify home page in js file

#7 Post by abc0502 » 21 Feb 2012 17:49

Problem solved here is the user.js file should be like that:

Code: Select all

user_pref("mail.server.default.abbreviate",true); // no newsgroup abbreviation

// use Google instead of Netscape for search
user_pref("browser.startup.homepage", "www.yahoo.com");

change the yahoo with any other start up page and put the user.js in profiles\ut5tyrywe.defualt folder (thaname will change).

then after a restart the page will be changed

even if the user changed the homepage from the browser when the firefox closed and re-opend again the user.js file seting is the one that came back and be come the default home page again


END

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: modify home page in js file

#8 Post by foxidrive » 21 Feb 2012 20:58

user.js may already exist in peoples profile so you could be damaging their profile if you simply overwrite theirs.

This will add that line to the bottom of user.js in every profile, and create the file if it doesn't exist.

If there is already a browser.startup.homepage line then Firefox might default to the last one in the user.js file - you can test that.

Code: Select all

@echo off
pushd "%userprofile%\Application Data\Mozilla\Firefox\Profiles"
for /f "delims=" %%a in ('dir /b /ad') do (
echo user_pref("browser.startup.homepage", "www.google.com");>>"%%a\user.js"
)
popd

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: modify home page in js file

#9 Post by abc0502 » 24 Feb 2012 10:50

thanks that's a very good idea iw ill try and let you know

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: modify home page in js file

#10 Post by foxidrive » 01 Mar 2012 05:30

abc0502 wrote:i wrote a question before about modifing hoom page using user.js file here:
http://www.dostips.com/forum/viewtopic.php?f=3&t=2965

aGerman and Foxidrive gived me the answer but i notice a problem the semicolen (;) at the end
of the user.js file refuse to appear i tried diffrent way any ideas


The error is in the ( ) in the line. They should be escaped.


Code: Select all

@echo off
pushd "%userprofile%\Application Data\Mozilla\Firefox\Profiles"
for /f "delims=" %%a in ('dir /b /ad') do (
echo user_pref^("browser.startup.homepage", "www.google.com"^);>>"%%a\user.js"
)
popd

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: modify home page in js file

#11 Post by abc0502 » 02 Mar 2012 17:15

Thanks and sorry for the other topic if you can delete it in order not to fill the forum,
and about ^ is there any explanation about the use of it i remember some one explained it before
but i don't know how to find the thread about it again thanks again

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: modify home page in js file

#12 Post by abc0502 » 02 Mar 2012 23:43

i used ^ in many casses and what i understood is this mark make chracters like )(/;%& appear in batch files is that right :?:

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: modify home page in js file

#13 Post by foxidrive » 03 Mar 2012 05:02

^ can escape any character but in this case there was a () pair in the for in do.

When you echo a closing ) then cmd sees it as a closing parenthesis in the for in do loop!
so the solution is to use ^) and cmd then knows it is a normal character.

I also used ^( with the leading bracket so that you can see where parentheses are matched and when they aren't. It matters in a much more complex batch file so it is a good habit to get into.

You also see " echo( " around here and there to echo a blank line and in my view it is a bad practice because syntax highlighting editors will show mismatched brackets too, and when you are trying to count pairs of brackets it causes an issue. Instead of echo( you can use echo. and echo/ etc but they all have issues sometimes, IIRC.

And yes, these will echo special characters to the console, and to a file.

echo ^|
echo ^&
echo ^>
echo ^<

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: modify home page in js file

#14 Post by abc0502 » 04 Mar 2012 00:31

Thanks :)

Post Reply