updater.bat 9.5 KB

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