updater.bat 9.4 KB

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