Skip to content

Instantly share code, notes, and snippets.

@vyach-vasiliev
Last active April 25, 2017 19:26
Show Gist options
  • Save vyach-vasiliev/c5c5467567fabc2b38a0b320d234b941 to your computer and use it in GitHub Desktop.
Save vyach-vasiliev/c5c5467567fabc2b38a0b320d234b941 to your computer and use it in GitHub Desktop.
Patch script for resources.pak of Google Chrome
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os, re
from grit.format import data_pack
def to_patch(patch_rules):
""" Patch for resources.pak file into part of Chrome browser
patch_rules - Dictionary with meanings
input_file - Path to input file (creates a backup)
output_file - Path to output file (if already exists - auto renames)
search_string - Search string (allows regex)
act - One of the options is valid (lower case): append, prepend, replace, remove
data - Data for change
"""
# TODO: check exits input and output files
data = data_pack.ReadDataPack(patch_rules['input_file'])
# print(data.encoding)
# PATCH PART
for (resource_id, text) in data.resources.iteritems():
# print('%s: %s' % (resource_id, len(text)))
if re.search(patch_rules['search_string'], text):
if patch_rules['act'] is 'append':
data.resources[resource_id] = text + patch_rules['data']
elif patch_rules['act'] is 'prepend':
data.resources[resource_id] = patch_rules['data'] + text
elif patch_rules['act'] is 'replace':
data.resources[resource_id] = re.sub(patch_rules['search_string'], patch_rules['data'], text)
elif patch_rules['act'] is 'remove':
data.resources[resource_id] = re.sub(patch_rules['search_string'], '', text)
print('In resource ID {} - {} - {} chars'.format(resource_id, patch_rules['act'], len(patch_rules['data'])))
break
# SAVE PART
if not os.path.exists(patch_rules['output_file']):
os.makedirs(patch_rules['output_file'])
if patch_rules['input_file'] == patch_rules['output_file']:
os.rename(patch_rules['input_file'], patch_rules['input_file']+'.bak')
if os.path.exists(patch_rules['output_file']):
i = 0
while os.path.exists(patch_rules['output_file']):
i += 1
patch_rules['output_file'] = ''.join(patch_rules['output_file'].split('.')[:-1]) + \
' ('+i+')' + patch_rules['output_file'].split('.')[-1]
data_pack.WriteDataPack(data.resources, patch_rules['output_file'], data.encoding)
if __name__ == '__main__':
# example
# Hides default icons of frequently visited sites
# and shows them when you hover the cursor on NewTab (in Google Chrome, Chromium and etc. with original NewTab page)
PATCH_RULES = {
'input_file': 'resources.pak',
'output_file': 'resources.pak',
'search_string': '#mv-tiles',
'act': 'append', # append, prepend, replace, remove
'data': '''
#mv-tiles {
opacity: 0;
}
#mv-tiles:hover {
opacity: 1;
}
''',
}
to_patch(PATCH_RULES)
''' HOW USE IT
$ git clone https://chromium.googlesource.com/chromium/src/tools/grit
-> Move this file in grit folder and edit
-> Move <Chrome-dir>/.../<version>/resources.pak file in grit folder
$ cd grit
$ python to_patch.py
-> file resources.pak is patched! File resources.pak.bak - backup original resources.pak file
-> Replace got file in dir <Chrome-dir>/.../<version>/
-> End.
/* The MIT License
Copyright (c) 2017 Vyacheslav Vasiliev (vyach.vasiliev\аt\gmail\dоt\com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. */
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment