updater.bat 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. @ECHO OFF & SETLOCAL EnableDelayedExpansion
  2. TITLE ghacks user.js updater
  3. REM ## ghacks-user.js updater for Windows
  4. REM ## author: @claustromaniac
  5. REM ## version: 4.8
  6. REM ## instructions: https://github.com/ghacksuserjs/ghacks-user.js/wiki/3.3-Updater-Scripts
  7. SET v=4.8
  8. VERIFY ON
  9. CD /D "%~dp0"
  10. SET _myname=%~n0
  11. SET _myparams=%*
  12. :parse
  13. IF "%~1"=="" (GOTO endparse)
  14. IF /I "%~1"=="-unattended" (SET _ua=1)
  15. IF /I "%~1"=="-log" (SET _log=1)
  16. IF /I "%~1"=="-logp" (SET _log=1 & SET _logp=1)
  17. IF /I "%~1"=="-multioverrides" (SET _multi=1)
  18. IF /I "%~1"=="-merge" (SET _merge=1)
  19. IF /I "%~1"=="-updatebatch" (SET _updateb=1)
  20. IF /I "%~1"=="-singlebackup" (SET _singlebackup=1)
  21. IF /I "%~1"=="-esr" (SET _esr=1)
  22. IF /I "%~1"=="-rfpalts" (SET _rfpalts=1)
  23. SHIFT
  24. GOTO parse
  25. :endparse
  26. IF DEFINED _updateb (
  27. REM The normal flow here goes from phase 1 to phase 2 and then phase 3.
  28. IF NOT "!_myname:~0,9!"=="[updated]" (
  29. IF EXIST "[updated]!_myname!.bat" (
  30. REM ## Phase 3 ##: The new script, with the original name, will:
  31. REM * Delete the [updated]*.bat and *.bat.old scripts
  32. REM * Begin the normal routine
  33. FC "[updated]!_myname!.bat" "!_myname!.bat.old" >nul
  34. IF NOT "!errorlevel!"=="0" (
  35. CALL :message "Script updated to version !v!"
  36. TIMEOUT 3 >nul
  37. )
  38. REN "[updated]!_myname!.bat" "[updated]!_myname!.bat.old"
  39. DEL /F "!_myname!.bat.old" "[updated]!_myname!.bat.old"
  40. GOTO begin
  41. )
  42. REM ## Phase 1 ##
  43. REM * Download new batch and name it [updated]*.bat
  44. REM * Start that script in a new CMD window
  45. REM * Exit
  46. CALL :message "Updating script..."
  47. REM Uncomment the next line and comment out the PowerShell call for testing.
  48. REM COPY /B /Y "!_myname!.bat" "[updated]!_myname!.bat" >nul
  49. (
  50. PowerShell -Command "(New-Object Net.WebClient).DownloadFile('https://raw.githubusercontent.com/ghacksuserjs/ghacks-user.js/master/updater.bat', '[updated]!_myname!.bat')"
  51. ) >nul 2>&1
  52. IF EXIST "[updated]!_myname!.bat" (
  53. START /min CMD /C "[updated]!_myname!.bat" !_myparams!
  54. ) ELSE (
  55. CALL :message "Failed. Make sure PowerShell is allowed internet access."
  56. TIMEOUT 120 >nul
  57. )
  58. ) ELSE (
  59. IF "!_myname!"=="[updated]" (
  60. CALL :message "The [updated] label is reserved. Rename this script and try again."
  61. TIMEOUT 300 >nul
  62. ) ELSE (
  63. REM ## Phase 2 ##: The [updated]*.bat script will:
  64. REM * Rename the old script and make a copy of itself with the original name.
  65. REM * Run that copy in a new CMD instance
  66. REM * Exit
  67. IF EXIST "!_myname:~9!.bat" ( REN "!_myname:~9!.bat" "!_myname:~9!.bat.old" )
  68. COPY /B /Y "!_myname!.bat" "!_myname:~9!.bat"
  69. START CMD /C "!_myname:~9!.bat" !_myparams!
  70. )
  71. )
  72. EXIT /B
  73. )
  74. :begin
  75. CLS
  76. ECHO:
  77. ECHO:
  78. ECHO: ########################################
  79. ECHO: #### user.js Updater for Windows ####
  80. ECHO: #### by claustromaniac ####
  81. ECHO: #### v!v! ####
  82. ECHO: ########################################
  83. ECHO:
  84. SET /A "_line=0"
  85. IF NOT EXIST user.js (
  86. CALL :message "user.js not detected in the current directory."
  87. ) ELSE (
  88. FOR /F "skip=1 tokens=1,* delims=:" %%G IN (user.js) DO (
  89. SET /A "_line+=1"
  90. IF !_line! GEQ 4 (GOTO exitloop)
  91. IF !_line! EQU 1 (SET _name=%%H)
  92. IF !_line! EQU 2 (SET _date=%%H)
  93. IF !_line! EQU 3 (SET _version=%%G)
  94. )
  95. :exitloop
  96. IF NOT "!_name!"=="" (
  97. IF /I NOT "!_name!"=="!_name:ghacks=!" (
  98. CALL :message "!_name! !_version:~2!,!_date!"
  99. ) ELSE (CALL :message "Current user.js version not recognised.")
  100. ) ELSE (CALL :message "Current user.js version not recognised.")
  101. )
  102. ECHO:
  103. IF NOT DEFINED _ua (
  104. CALL :message "This batch should be run from your Firefox profile directory."
  105. ECHO: It will download the latest version of ghacks user.js from github and then
  106. CALL :message "append any of your own changes from user-overrides.js to it."
  107. CALL :message "Visit the wiki for more detailed information."
  108. ECHO:
  109. TIMEOUT 1 /nobreak >nul
  110. CHOICE /C SHE /N /M "Start [S] Help [H] Exit [E]"
  111. CLS
  112. IF ERRORLEVEL 3 (EXIT /B)
  113. IF ERRORLEVEL 2 (GOTO :showhelp)
  114. )
  115. IF DEFINED _log (
  116. CALL :log >>user.js-update-log.txt 2>&1
  117. IF DEFINED _logp (START user.js-update-log.txt)
  118. EXIT /B
  119. :log
  120. SET _log=2
  121. ECHO:##################################################################
  122. ECHO: %date%, %time%
  123. )
  124. IF EXIST user.js.new (DEL /F "user.js.new")
  125. CALL :message "Retrieving latest user.js file from github repository..."
  126. (
  127. PowerShell -Command "(New-Object Net.WebClient).DownloadFile('https://raw.githubusercontent.com/ghacksuserjs/ghacks-user.js/master/user.js', 'user.js.new')"
  128. ) >nul 2>&1
  129. IF EXIST user.js.new (
  130. IF DEFINED _esr (
  131. CALL :message "Activating ESR section..."
  132. CALL :esr user.js.new
  133. )
  134. IF DEFINED _rfpalts (
  135. CALL :message "Activating RFP Alternatives section..."
  136. CALL :rfpalts user.js.new
  137. )
  138. IF DEFINED _multi (
  139. FORFILES /P user.js-overrides /M *.js >nul 2>&1
  140. IF NOT ERRORLEVEL 1 (
  141. IF DEFINED _merge (
  142. CALL :message "Merging..."
  143. COPY /B /Y user.js-overrides\*.js user-overrides-merged.js
  144. CALL :merge user-overrides-merged.js
  145. COPY /B /Y user.js.new+user-overrides-merged.js user.js.new
  146. CALL :merge user.js.new
  147. ) ELSE (
  148. CALL :message "Appending..."
  149. COPY /B /Y user.js.new+"user.js-overrides\*.js" user.js.new
  150. )
  151. ) ELSE (CALL :message "No override files found.")
  152. ) ELSE (
  153. IF EXIST "user-overrides.js" (
  154. COPY /B /Y user.js.new+"user-overrides.js" "user.js.new"
  155. IF DEFINED _merge (
  156. CALL :message "Merging user-overrides.js..."
  157. CALL :merge user.js.new
  158. ) ELSE (
  159. CALL :message "user-overrides.js appended."
  160. )
  161. ) ELSE (CALL :message "user-overrides.js not found.")
  162. )
  163. IF EXIST user.js (
  164. FC user.js.new user.js >nul && SET "_changed=false" || SET "_changed=true"
  165. )
  166. IF "!_changed!"=="true" (
  167. CALL :message "Backing up..."
  168. IF DEFINED _singlebackup (
  169. MOVE /Y user.js user.js.bak >nul
  170. ) ELSE (
  171. SET "_time=!time: =0!"
  172. MOVE /Y user.js "user-backup-!date:/=-!_!_time::=.!.js" >nul
  173. )
  174. REN user.js.new user.js
  175. CALL :message "Update complete."
  176. ) ELSE (
  177. IF "!_changed!"=="false" (
  178. DEL /F user.js.new >nul
  179. CALL :message "Update completed without changes."
  180. ) ELSE (
  181. REN user.js.new user.js
  182. CALL :message "Update complete."
  183. SET "_changed=true"
  184. )
  185. )
  186. ) ELSE (
  187. CALL :message "Update failed. Make sure PowerShell is allowed internet access."
  188. ECHO: No changes were made.
  189. )
  190. IF NOT DEFINED _log (
  191. IF NOT DEFINED _ua (
  192. IF EXIST prefsCleaner.bat (
  193. IF "!_changed!"=="true" (
  194. CALL :message "Would you like to run the prefsCleaner now?"
  195. CHOICE /C YN /N /M "(Y/N) "
  196. IF "1"=="!errorlevel!" ( START "" cmd.exe /C "prefsCleaner.bat" )
  197. ) ELSE (PAUSE)
  198. ) ELSE (PAUSE)
  199. )
  200. )
  201. EXIT /B
  202. REM ########### Message Function ###########
  203. :message
  204. SETLOCAL DisableDelayedExpansion
  205. IF NOT "2"=="%_log%" (ECHO:)
  206. ECHO: %~1
  207. IF NOT "2"=="%_log%" (ECHO:)
  208. ENDLOCAL
  209. GOTO :EOF
  210. REM ############ ESR Function ############
  211. :esr
  212. SETLOCAL DisableDelayedExpansion
  213. (
  214. FOR /F "tokens=1,* delims=:" %%G IN ('FINDSTR /N "^" "%~1"') DO (
  215. SET "_temp=%%H"
  216. SETLOCAL EnableDelayedExpansion
  217. IF NOT "!_temp:~-37!"==".x still uses all the following prefs" (
  218. ENDLOCAL & ECHO:%%H
  219. ) ELSE (
  220. ECHO://!_temp:~2!
  221. ENDLOCAL
  222. )
  223. )
  224. )>updatertempfile
  225. MOVE /Y updatertempfile "%~1" >nul
  226. ENDLOCAL
  227. GOTO :EOF
  228. REM ############ RFP Alts Function ############
  229. :rfpalts
  230. SETLOCAL DisableDelayedExpansion
  231. (
  232. FOR /F "tokens=1,* delims=:" %%G IN ('FINDSTR /N "^" "%~1"') DO (
  233. SET "_temp=%%H"
  234. SETLOCAL EnableDelayedExpansion
  235. IF "!_temp:[SETUP-non-RFP]=!"=="!_temp!" (
  236. ENDLOCAL & ECHO:%%H
  237. ) ELSE (
  238. ECHO://!_temp:~2!
  239. ENDLOCAL
  240. )
  241. )
  242. )>updatertempfile
  243. MOVE /Y updatertempfile "%~1" >nul
  244. ENDLOCAL
  245. GOTO :EOF
  246. REM ############ Merge Function ############
  247. :merge
  248. SETLOCAL DisableDelayedExpansion
  249. FOR /F tokens^=2^,^*^ delims^=^'^" %%G IN ('FINDSTR /R /C:"^user_pref[ ]*\([ ]*[\"'].*[\"'][ ]*,.*\)[ ]*;" "%~1"') DO (SET "[%%G]=%%H")
  250. FOR /F tokens^=2^,^*^ delims^=^' %%G IN ('FINDSTR /R /C:"^//// --- comment-out --- '[^'][^']*'.*" "%~1"') DO (SET "__unset__%%G=1")
  251. (
  252. FOR /F "tokens=1,* delims=:" %%I IN ('FINDSTR /N "^" "%~1"') DO (
  253. SET "_temp=%%J"
  254. SETLOCAL EnableDelayedExpansion
  255. IF NOT "!_temp:~0,9!"=="user_pref" (
  256. ENDLOCAL & ECHO:%%J
  257. ) ELSE (
  258. IF "!_temp:;=!"=="!_temp!" (
  259. ENDLOCAL & ECHO:%%J
  260. ) ELSE (
  261. ENDLOCAL
  262. FOR /F tokens^=2^ delims^=^'^" %%K IN ("%%J") DO (
  263. IF NOT "_user.js.parrot"=="%%K" (
  264. IF DEFINED __unset__%%K (
  265. ECHO://%%J
  266. ) ELSE (
  267. IF DEFINED [%%K] (
  268. SETLOCAL EnableDelayedExpansion
  269. FOR /F "delims=" %%L IN ("![%%K]!") DO (
  270. ENDLOCAL & ECHO:user_pref("%%K"%%L
  271. SET "[%%K]="
  272. )
  273. )
  274. )
  275. ) ELSE (ECHO:%%J)
  276. )
  277. )
  278. )
  279. )
  280. )>updatertempfile
  281. MOVE /Y updatertempfile "%~1" >nul
  282. ENDLOCAL
  283. GOTO :EOF
  284. REM ############### Help ##################
  285. :showhelp
  286. MODE 80,53
  287. CLS
  288. CALL :message "Available arguments (case-insensitive):"
  289. CALL :message " -esr"
  290. ECHO: Activate ESR related preferences
  291. CALL :message " -log"
  292. ECHO: Write the console output to a logfile (user.js-update-log.txt)
  293. CALL :message " -logP"
  294. ECHO: Like -log, but also open the logfile after updating.
  295. CALL :message " -merge"
  296. ECHO: Merge overrides instead of appending them. Single-line comments and
  297. ECHO: _user.js.parrot lines are appended normally. Overrides for inactive
  298. ECHO: user.js prefs will be appended. When -Merge and -MultiOverrides are used
  299. ECHO: together, a user-overrides-merged.js file is also generated in the root
  300. ECHO: directory for quick reference. It contains only the merged data from
  301. ECHO: override files and can be safely discarded after updating, or used as the
  302. ECHO: new user-overrides.js. When there are conflicting records for the same
  303. ECHO: pref, the value of the last one declared will be used. Visit the wiki
  304. ECHO: for usage examples and more detailed information.
  305. CALL :message " -multiOverrides"
  306. ECHO: Use any and all .js files in a user.js-overrides sub-folder as overrides
  307. ECHO: instead of the default user-overrides.js file. Files are appended in
  308. ECHO: alphabetical order.
  309. CALL :message " -unattended"
  310. ECHO: Run without user input.
  311. CALL :message " -singleBackup"
  312. ECHO: Use a single backup file and overwrite it on new updates, instead of
  313. ECHO: cumulative backups. This was the default behaviour before v4.3.
  314. CALL :message " -rfpAlts"
  315. ECHO: Activate RFP Alternatives section
  316. CALL :message " -updateBatch"
  317. ECHO: Update the script itself on execution, before the normal routine.
  318. CALL :message ""
  319. PAUSE
  320. MODE 80,25
  321. GOTO :begin