def add_action(action): """ Add an ida-action :param action: action given as the `Action` namedtuple :return: None """ class Handler(ida_kernwin.action_handler_t): def __init__(self): ida_kernwin.action_handler_t.__init__(self) def activate(self, ctx): action.handler() return 1 def update(self, ctx): return ida_kernwin.AST_ENABLE_FOR_WIDGET act_icon = -1 if action.icon_filename: icon_full_filename = \ pkg_resources.resource_filename('fa', os.path.join( 'res', 'icons', action.icon_filename)) with open(icon_full_filename, 'rb') as f: icon_data = f.read() act_icon = ida_kernwin.load_custom_icon(data=icon_data, format="png") act_name = action.name ida_kernwin.unregister_action(act_name) if ida_kernwin.register_action(ida_kernwin.action_desc_t( act_name, # Name. Acts as an ID. Must be unique. action.label, # Label. That's what users see. Handler(), # Handler. Called when activated, and for updating action.hotkey, # Shortcut (optional) None, # Tooltip (optional) act_icon)): # Icon ID (optional) # Insert the action in the menu if not ida_kernwin.attach_action_to_menu( "FA/", act_name, ida_kernwin.SETMENU_APP): print("Failed attaching to menu.") # Insert the action in a toolbar if not ida_kernwin.attach_action_to_toolbar("fa", act_name): print("Failed attaching to toolbar.") class Hooks(ida_kernwin.UI_Hooks): def finish_populating_widget_popup(self, widget, popup): if ida_kernwin.get_widget_type(widget) == \ ida_kernwin.BWN_DISASM: ida_kernwin.attach_action_to_popup(widget, popup, act_name, None) hooks = Hooks() hooks.hook()
def register(self): r = ida_kernwin.register_action(self.get_desc()) if not r: log('actions').warning("failed registering %s: %s", self, r) return ida_kernwin.attach_action_to_menu(self.get_action_path(), self.get_id(), ida_kernwin.SETMENU_APP) r = ida_kernwin.attach_action_to_toolbar("AnalysisToolBar", self.get_id()) if not r: log('actions').warn("registration of %s failed: %s", self, r)
SayHi("developer" ), # Handler. Called when activated, and for updating "Ctrl+F12", # Shortcut (optional) "Greets the user", # Tooltip (optional) act_icon)): # Icon ID (optional) print("Action registered. Attaching to menu.") # Insert the action in the menu if ida_kernwin.attach_action_to_menu("Edit/Export data", act_name, ida_kernwin.SETMENU_APP): print("Attached to menu.") else: print("Failed attaching to menu.") # Insert the action in a toolbar if ida_kernwin.attach_action_to_toolbar("AnalysisToolBar", act_name): print("Attached to toolbar.") else: print("Failed attaching to toolbar.") # We will also want our action to be available in the context menu # for the "IDA View-A" widget. # # To do that, we could in theory retrieve a reference to "IDA View-A", and # then request to "permanently" attach the action to it, using something # like this: # ida_kernwin.attach_action_to_popup(ida_view_a, None, act_name, None) # # but alas, that won't do: widgets in IDA are very "volatile", and # can be deleted & re-created on some occasions (e.g., starting a # debugging session), and our efforts to permanently register our