fixed type pref

This commit is contained in:
partisan 2024-12-08 18:22:32 +01:00
parent 6c9c66bd7a
commit bb37c8e2ad
3 changed files with 12 additions and 8 deletions

View file

@ -1,4 +1,4 @@
t:pref
t: pref
i: /browser/app/profile/firefox.js
o: /browser/app/profile/firefox.js

View file

@ -1,4 +1,4 @@
t:pref
t: pref
i: /browser/app/profile/firefox.js
o: /browser/app/profile/firefox.js

16
pref.go
View file

@ -29,10 +29,12 @@ func applyPrefModifications(targetFilePath string, modifications []string) error
// Prepare the modification map
for _, mod := range modifications {
if strings.HasPrefix(mod, "+pref") {
parts := strings.SplitN(mod, "(", 2)
if len(parts) > 1 {
key := strings.TrimSpace(parts[0][1:])
modMap[key] = strings.TrimSpace(mod)
// Strip the `+` and extract the key within parentheses
start := strings.Index(mod, "(")
end := strings.LastIndex(mod, ")")
if start > 0 && end > start {
key := strings.TrimSpace(mod[start+1 : end])
modMap[key] = strings.TrimPrefix(mod, "+") // Remove the `+` prefix
}
}
}
@ -44,7 +46,7 @@ func applyPrefModifications(targetFilePath string, modifications []string) error
// Check if the line matches any modification key
lineModified := false
for key, replacement := range modMap {
if strings.HasPrefix(line, key) && !modifiedLines[key] {
if strings.Contains(line, key) && !modifiedLines[key] {
fmt.Printf("Replacing line: %s\n", line)
if _, err := tempFile.WriteString(replacement + "\n"); err != nil {
return fmt.Errorf("failed to write to temp file: %v", err)
@ -70,7 +72,9 @@ func applyPrefModifications(targetFilePath string, modifications []string) error
// Write remaining modifications (new preferences)
for key, replacement := range modMap {
if !modifiedLines[key] {
if _, err := tempFile.WriteString(replacement + "\n"); err != nil {
// Remove the `+` prefix before writing
cleanReplacement := strings.TrimPrefix(replacement, "+")
if _, err := tempFile.WriteString(cleanReplacement + "\n"); err != nil {
return fmt.Errorf("failed to write new preference to temp file: %v", err)
}
}