updater.bat 10 KB

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