updater.bat 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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.10
  6. REM ## instructions: https://github.com/ghacksuserjs/ghacks-user.js/wiki/3.3-Updater-Scripts
  7. SET v=4.10
  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 _rfpalts (
  131. CALL :message "Activating RFP Alternatives section..."
  132. CALL :activate user.js.new "[SETUP-non-RFP]"
  133. )
  134. IF DEFINED _esr (
  135. CALL :message "Activating ESR section..."
  136. CALL :activate user.js.new ".x still uses all the following prefs"
  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. ::::::::::::::: Message :::::::::::::::
  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. ::::::::::::::: Activate Section :::::::::::::::
  211. :activate
  212. :: arg1 = file
  213. :: arg2 = line substring
  214. SETLOCAL DisableDelayedExpansion
  215. (
  216. FOR /F "tokens=1,* delims=:" %%G IN ('FINDSTR /N "^" "%~1"') DO (
  217. SET "_temp=%%H"
  218. SETLOCAL EnableDelayedExpansion
  219. IF "!_temp:%~2=!"=="!_temp!" (
  220. ENDLOCAL & ECHO:%%H
  221. ) ELSE (
  222. ECHO://!_temp:~2!
  223. ENDLOCAL
  224. )
  225. )
  226. )>updatertempfile
  227. MOVE /Y updatertempfile "%~1" >nul
  228. ENDLOCAL
  229. GOTO :EOF
  230. ::::::::::::::: Merge :::::::::::::::
  231. :merge
  232. SETLOCAL DisableDelayedExpansion
  233. FOR /F tokens^=2^,^*^ delims^=^'^" %%G IN ('FINDSTR /R /C:"^user_pref[ ]*\([ ]*[\"'].*[\"'][ ]*,.*\)[ ]*;" "%~1"') DO (SET "[%%G]=%%H")
  234. FOR /F tokens^=2^,^*^ delims^=^' %%G IN ('FINDSTR /R /C:"^//// --- comment-out --- '[^'][^']*'.*" "%~1"') DO (SET "__unset__%%G=1")
  235. (
  236. FOR /F "tokens=1,* delims=:" %%I IN ('FINDSTR /N "^" "%~1"') DO (
  237. SET "_temp=%%J"
  238. SETLOCAL EnableDelayedExpansion
  239. IF NOT "!_temp:~0,9!"=="user_pref" (
  240. ENDLOCAL & ECHO:%%J
  241. ) ELSE (
  242. IF "!_temp:;=!"=="!_temp!" (
  243. ENDLOCAL & ECHO:%%J
  244. ) ELSE (
  245. ENDLOCAL
  246. FOR /F tokens^=2^ delims^=^'^" %%K IN ("%%J") DO (
  247. IF NOT "_user.js.parrot"=="%%K" (
  248. IF DEFINED __unset__%%K (
  249. ECHO://%%J
  250. ) ELSE (
  251. IF DEFINED [%%K] (
  252. SETLOCAL EnableDelayedExpansion
  253. FOR /F "delims=" %%L IN ("![%%K]!") DO (
  254. ENDLOCAL & ECHO:user_pref("%%K"%%L
  255. SET "[%%K]="
  256. )
  257. )
  258. )
  259. ) ELSE (ECHO:%%J)
  260. )
  261. )
  262. )
  263. )
  264. )>updatertempfile
  265. MOVE /Y updatertempfile "%~1" >nul
  266. ENDLOCAL
  267. GOTO :EOF
  268. ::::::::::::::: Help :::::::::::::::
  269. :showhelp
  270. MODE 80,54
  271. CLS
  272. CALL :message "Available arguments (case-insensitive):"
  273. CALL :message " -esr"
  274. ECHO: Activate ESR related preferences
  275. CALL :message " -log"
  276. ECHO: Write the console output to a logfile (user.js-update-log.txt)
  277. CALL :message " -logP"
  278. ECHO: Like -log, but also open the logfile after updating.
  279. CALL :message " -merge"
  280. ECHO: Merge overrides instead of appending them. Single-line comments and
  281. ECHO: _user.js.parrot lines are appended normally. Overrides for inactive
  282. ECHO: user.js prefs will be appended. When -Merge and -MultiOverrides are used
  283. ECHO: together, a user-overrides-merged.js file is also generated in the root
  284. ECHO: directory for quick reference. It contains only the merged data from
  285. ECHO: override files and can be safely discarded after updating, or used as the
  286. ECHO: new user-overrides.js. When there are conflicting records for the same
  287. ECHO: pref, the value of the last one declared will be used. Visit the wiki
  288. ECHO: for usage examples and more detailed information.
  289. CALL :message " -multiOverrides"
  290. ECHO: Use any and all .js files in a user.js-overrides sub-folder as overrides
  291. ECHO: instead of the default user-overrides.js file. Files are appended in
  292. ECHO: alphabetical order.
  293. CALL :message " -unattended"
  294. ECHO: Run without user input.
  295. CALL :message " -singleBackup"
  296. ECHO: Use a single backup file and overwrite it on new updates, instead of
  297. ECHO: cumulative backups. This was the default behaviour before v4.3.
  298. CALL :message " -rfpAlts"
  299. ECHO: Activate RFP Alternatives section
  300. CALL :message " -updateBatch"
  301. ECHO: Update the script itself on execution, before the normal routine.
  302. CALL :message ""
  303. PAUSE
  304. MODE 80,25
  305. GOTO :begin