Page 1 of 1

modify home page in js file [solved]

Posted: 21 Feb 2012 10:38
by abc0502
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

Re: modify home page in js file

Posted: 21 Feb 2012 14:37
by aGerman
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

Re: modify home page in js file

Posted: 21 Feb 2012 15:17
by abc0502
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

Re: modify home page in js file

Posted: 21 Feb 2012 15:25
by aGerman
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

Re: modify home page in js file

Posted: 21 Feb 2012 16:43
by abc0502
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

Re: modify home page in js file

Posted: 21 Feb 2012 17:22
by abc0502
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 :(

Re: modify home page in js file

Posted: 21 Feb 2012 17:49
by abc0502
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

Re: modify home page in js file

Posted: 21 Feb 2012 20:58
by foxidrive
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

Re: modify home page in js file

Posted: 24 Feb 2012 10:50
by abc0502
thanks that's a very good idea iw ill try and let you know

Re: modify home page in js file

Posted: 01 Mar 2012 05:30
by foxidrive
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

Re: modify home page in js file

Posted: 02 Mar 2012 17:15
by abc0502
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

Re: modify home page in js file

Posted: 02 Mar 2012 23:43
by abc0502
i used ^ in many casses and what i understood is this mark make chracters like )(/;%& appear in batch files is that right :?:

Re: modify home page in js file

Posted: 03 Mar 2012 05:02
by foxidrive
^ 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 ^<

Re: modify home page in js file

Posted: 04 Mar 2012 00:31
by abc0502
Thanks :)