📜 ⬆️ ⬇️

We programmatically remove fields and headers and headers in Firefox under Windows

Why do you need it?


Our company uses a web application, one of the possibilities of which is to print completed forms. The authorities are very furious when they bring documents with browser headers and footers. The web programmer is made guilty while he is responsible for the web application.
System administrators are aware of this case. But the browser zoo is represented by several species of individuals. Although when installing them, the footers and fields are removed, the evil doesn’t sleep and black magic does its job - the documents are printed with the footers and you have to clean them again manually. “To Kohl?” Asks our IT department bosses.


Other solutions to the problem described?


Oh yeah! Transfer documents to pdf and print them, as their viewers are installed by default without headers and footers. As a web programmer, I was assigned this. I suspected that this decision was bad, but I did not dare to say to myself without trying it. I don’t know where I got this habit from, but I don’t convince the customer that he really doesn’t need his demand until I’m sure that it’s not my incompetence that pushes me. In other words, I do not refuse the task, explaining it by the lack of necessity instead of the presence of my laziness. In general, it started. mPDF, fpdf, tcpdf, dom_pdf. I first tried mPDF - it coped with the layout more precisely than the rest without any dancing with styles, but, unfortunately, did not support scripts for auto printing when opened. Saved dom_pdf, achieved full compliance and auto-print when opened. Having proved to myself that my laziness in solving a problem through pdf has nothing to do with it, I began to look for other solutions.

Get to the point!


After digging into the Firefox files installed on Windows XP, you can find the prefs.js file. A quick search by content showed the presence of parameters very similar to those sought:
')
user_pref("print.printer_\\\\--\\HP_Universal_Printing_PCL_5.print_footerleft", ""); user_pref("print.printer_\\\\--\\HP_Universal_Printing_PCL_5.print_margin_right", "0"); 

There are several such lines.

Nothing smarter, I did not figure out how to start google "cmd replace text" and the like. Even console utilities found gsar, fart. The meaning of life is to replace the text in the files with the desired one. But there were two problems that I could not solve: how to pass strings containing quotation marks, and which string to search for a replacement.

That's how I looked a little bit into the world of VBScript.

 REM       prefs.js  Firefox ' ProcessKillLocal.vbs ' Sample VBScript to kill a program ' Author Guy Thomas http://computerperformance.co.uk/ ' Version 2.7 - December 2010 ' ------------------------ -------------------------------' Dim objWMIService, objProcess, colProcess Dim strComputer, strProcessKill strComputer = "." strProcessKill = "'firefox.exe'" Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & strComputer & "\root\cimv2") Set colProcess = objWMIService.ExecQuery _ ("Select * from Win32_Process Where Name = " & strProcessKill ) For Each objProcess in colProcess objProcess.Terminate() Next WSCript.Echo "Just killed process " & strProcessKill _ & " on " & strComputer REM ---------------- ------------------------- Const ForReading = 1 Const ForWriting = 2 set re = New RegExp re.IgnoreCase = true re.Global = true re.Multiline = true REM   .         APPDATA Set objFSO = CreateObject("Scripting.FileSystemObject") Set oShell = CreateObject( "WScript.Shell" ) appdir=oShell.ExpandEnvironmentStrings("%APPDATA%") Set objFile = objFSO.OpenTextFile(appdir & "\Mozilla\Firefox\profiles.ini", ForReading) profile_str = objFile.ReadAll objFile.Close re.Pattern = "(Profiles/.+\w)$" Set mt = re.Execute(profile_str) profile_path = mt(0).Value ReDim PrefsFile(4) PrefsFile(0) = appdir PrefsFile(1) = "\Mozilla\Firefox\" PrefsFile(2) = profile_path PrefsFile(3) = "\prefs.js" strFileName = Join(PrefsFile, "") Set objFile = objFSO.OpenTextFile(strFileName, ForReading) strText = objFile.ReadAll objFile.Close REM "(\.print_margin_bottom"", "")(.+)(""\);)$" -  .print_margin_bottom", "0"); Dim SearchPtStr(6) SearchPtStr(0) = "print_footercenter" SearchPtStr(1) = "print_footerleft" SearchPtStr(2) = "print_footerright" SearchPtStr(3) = "print_headercenter" SearchPtStr(4) = "print_headerleft" SearchPtStr(5) = "print_headerright" For Each str In SearchPtStr re.Pattern = "(\." & str & """, "")(.+)(""\);)$" Set myMatches = re.Execute(strText) For Each myMatch in myMatches If Len(myMatch.Value)>1 Then strText = re.Replace(strText, "$1$3") End if Next Next Dim SearchPtInt(4) SearchPtInt(0) = "print_margin_bottom" SearchPtInt(1) = "print_margin_left" SearchPtInt(2) = "print_margin_right" SearchPtInt(3) = "print_margin_top" For Each str In SearchPtInt re.Pattern = "(\." & str & """, "")(.+)(""\);)$" Set myMatches = re.Execute(strText) For Each myMatch in myMatches If Len(myMatch.Value)>1 Then strText = re.Replace(strText, "$10$3") End if Next Next Set objFile = objFSO.OpenTextFile(strFileName, ForWriting) objFile.WriteLine strText objFile.Close 


In the first part of the script, we terminate the firefox.exe process, since when it exits, it overwrites prefs.js. The code snippet for this was booted and copied almost unchanged.
In the next part of the script, we read the file Application Data \ Mozilla \ Firefox \ profiles.ini,
where the profile path is stored, save to profile_path. Then, in this path, we load the contents of the settings file into the strText variable. Next, using a loop and regular expression
"(\." & str & "" "," ") (. +) (" "\);) $"
we find lines similar to
".print_margin_bottom", "0"); "


(there may be many such lines in the settings file, depending on the number of printers). We replace the found entries with $ 1 $ 3 for string parameters (footers) and $ 10 $ 3 for numeric parameters. Those. do either empty quotes or 0 in quotes.
we replace the string after the replacement into the file.

There is a strange place in the program:
If Len(myMatch.Value)>1 Then
strText = re.Replace(strText, "$1$3")
End if

When re.Global = true and such a regular expression comes up with empty matches, replacing which turns the settings file into a bloody melevo. A condition has been added to exclude it. I suspect that the error in the regular expression, but I could not resolve this issue myself.

Call the script, for example, rem_headers_ff.vbs and can be run from the command line
cscript rem_headers_ff.vbs


How does this solve the original problem?


IE footer settings are stored in the registry, which is easy to change programmatically.
We learned to rewrite the FF settings.
For some reason, Chrome doesn't use anyone else here anymore.
There is only one small step left - to solve the problem for Opera.

Source: https://habr.com/ru/post/131614/


All Articles