1.11.0 • Published 7 months ago

mousehunt-utils v1.11.0

Weekly downloads
-
License
MIT
Repository
-
Last release
7 months ago

🐭️ MouseHunt - MouseHunt Utils

A helper library for MouseHunt userscripts.

GitHub GitHub tag (latest SemVer)

Current Version: 1.10.5

Usage

Add the following to your userscript in the header.

// @require https://cdn.jsdelivr.net/npm/mousehunt-utils@1.10.5/mousehunt-utils.js

Your userscript should have a header like this:

// ==UserScript==
// @name         My Userscript
// @description  This is my userscript.
// @require      https://cdn.jsdelivr.net/npm/mousehunt-utils@1.10.5/mousehunt-utils.js
// @match        https://www.mousehuntgame.com/*
// ==/UserScript==

Once you've added the @require line, you can use the functions in the library with no extra setup!

Available functions

Triggers

  • onRequest Do something when a request is made.
  • onAjaxRequest Do something when a request is made. Deprecated: Use onRequest instead.
  • onEvent Do something when an event is triggered.
  • onPageChange Do something when the page changes.
  • onDialogShow Do something when a popup or overlay shows.
  • onDialogHide Do something when a popup or overlay hides.
  • onOverlayChange Do something when a popup or overlay changes. Deprecated: Use onDialogShow and onDialogHide instead.
  • onTrapChange Do something when a trap component changes, such as the charm or cheese.
  • onTravel Do something when the user travels to a new location.

Data

  • doRequest Make a request to a MouseHunt endpoint.

Page and User Data

Popups

Settings

Miscellaneous

Debugging

  • clog Log a message to the console.
  • debug Log a message to the console if debugging is enabled.
  • enableDebugMode Enable debugging.

onRequest

Do something when a request is made, either on every request or on a specific endpoint.

Parameters

NameTypeRequiredDescription
callbackfunctionYesThe function to call when the request is made.
urlstringNoThe URL to call the function on. If not specified, the function will be called on every request.
skipSuccessbooleanNoIf true, the function will not be called on successful requests.

Examples

onRequest(() => {
  // Do something when the userInventory endpoint is called.
}, 'managers/ajax/users/userInventory.php');

// Doing something on all requests.
onRequest((req) => { console.log(req) })

onAjaxRequest

Do something when a request is made, either on every request or on a specific endpoint. Deprecated: Use onRequest instead.

Parameters

NameTypeRequiredDescription
callbackfunctionYesThe function to call when the request is made.
urlstringNoThe URL to call the function on. If not specified, the function will be called on every request.
skipSuccessbooleanNoIf true, the function will not be called on successful requests.

Examples

onAjaxRequest(() => {
  // Do something when the userInventory endpoint is called.
}, 'managers/ajax/users/userInventory.php');

// Doing something on all requests.
onAjaxRequest((req) => { console.log(req) })

onEvent

Do something when an event is triggered - a wrapper around the HG event registry.

Parameters

NameTypeRequiredDescription
eventstringYesThe name of the event to listen for.
callbackfunctionYesThe function to call when the event is triggered.

Examples

onEvent('travel_complete', () => {
  // Do something when the user travels.
});
onEvent('ajax_response', () => {
  console.log('Fired when an ajax request is made.');
});

onPageChange

Do something when the page changes, either on any page or when leaving or entering a specific page.

Parameters

All parameters are optional, but you probably want to specify at least one of change, show, or hide.

NameTypeRequiredDescription
optionsobjectYesThe options for the trigger.
options.changefunctionNoThe function to call when the page changes.
options.<page>objectNoThe options for a specific page.
options.<page>.showfunctionNoThe function to call when a specifc page is shown.
options.<page>.hidefunctionNoThe function to call when a specifc page is hidden.

Available pages

Any of these can be used as the <page> parameter.

  • camp
  • travel
  • inventory
  • shop
  • mice
  • friends
  • sendSupplies
  • team
  • tournament
  • news
  • scoreboards
  • discord

Examples

onPageChange({
  change: () => { console.log('Current page: ' + getCurrentPage()); },
  travel: {
    show: () => { console.log('travel is visible'); },
    hide: () => { console.log('travel is hidden'); }
  },
});

onDialogShow

Do something when a popup / overlay shows, either on any popup or when a specific popup is shown.

Parameters

NameTypeRequiredDescription
callbackfunctionYesThe function to call when the popup is shown.
overlaystringNoThe popup to listen for.
oncebooleanNoWhether to only call the callback once.

Available Popups / Overlays

Any of these can be used as the <overlay> parameter, as well as passing in any other overlay name.

  • map
  • item
  • mouse
  • image
  • convertible
  • adventureBook
  • marketplace
  • gifts
  • support
  • premiumShop

Examples

onDialogShow(() => {
    console.log('Opened map popup');
  }, 'map');

onDialogHide

Do something when a popup / overlay hides, either on any popup or when a specific popup is hidden.

Parameters

NameTypeRequiredDescription
callbackfunctionYesThe function to call when the popup is hidden.
overlaystringNoThe popup to listen for.
oncebooleanNoWhether to only call the callback once.

Available Popups / Overlays

Any of these can be used as the <overlay> parameter, as well as passing in any other overlay name.

  • map
  • item
  • mouse
  • image
  • convertible
  • adventureBook
  • marketplace
  • gifts
  • support
  • premiumShop

Examples

onDialogHide(() => {
    console.log('Closed map popup');
  }, 'map');

onOverlayChange

Deprecated: Use onDialogShow and onDialogHide instead.

Do something when a popup / overlay changes, either on any popup or when a specific popup is shown or hidden.

Parameters

All parameters are optional, but you probably want to specify at least one of change, show, or hide.

NameTypeRequiredDescription
optionsobjectYesThe options for the trigger.
options.changefunctionNoThe function to call when the page changes.
options.<overlay>objectNoThe options for a specific overlay.
options.<overlay>.showfunctionNoThe function to call when a specifc overlay is shown.
options.<overlay>.hidefunctionNoThe function to call when a specifc overlay is hidden.

Available Popups / Overlays

Any of these can be used as the <overlay> parameter.

  • map
  • item
  • mouse
  • image
  • convertible
  • adventureBook
  • marketplace
  • gifts
  • support
  • premiumShop

Examples

onOverlayChange({
  change: () => { console.log('overlayPopup changed'); },
  show: () => { console.log('overlayPopup is visible'); },
  hide: () => { console.log('overlayPopup is hidden'); },
  map: {
    show: () => { console.log('treasureMapPopup is visible'); },
    hide: () => { console.log('treasureMapPopup is hidden'); },
  },
});

onTrapChange

Do something when a trap component changes, such as the charm or cheese.

Parameters

All parameters are optional, but you probably want to specify at least one of change, show, or hide.

NameTypeRequiredDescription
optionsobjectYesThe options for the trigger.
options.changefunctionNoThe function to call when the trap changes.
options.showfunctionNoThe function to call when the trap is shown.
options.hidefunctionNoThe function to call when the trap is hidden.
options.<component>objectNoThe options for a specific component.
options.<component>.changefunctionNoThe function to call when a specifc component changes.
options.<component>.showfunctionNoThe function to call when a specifc component is shown.
options.<component>.hidefunctionNoThe function to call when a specifc component is hidden.

Available Components

  • bait
  • base
  • weapon
  • charm
  • skin

Examples

onTrapChange({
  change: () => { console.log('trap changed'); },
  show: () => { console.log('trap is visible'); },
  hide: () => { console.log('trap is hidden'); },
  bait: {
    show: () => { console.log('bait is visible'); },
    hide: () => { console.log('bait is hidden'); }
  },
});

onTravel

Do something when the user travels to a new location.

Parameters

NameTypeRequiredDescription
locationstringYesThe location to travel to.
optionsobjectNoThe options for the trigger.
options.shouldAddReminderbooleanNoWhether to add a reminder for the location.
options.titlestringNoThe title of the reminder.
options.textstringNoThe text of the reminder.
options.buttonstringNoThe text of the button on the reminder.
options.actionfunctionNoThe function to call when the button is clicked.
options.callbackfunctionNoThe function to call when the travel is complete.

Examples

onTravel('table_of_contents', {
  shouldAddReminder: true,
  title: 'Make sure you enable CC!',
  text: 'If you don\'t enable CC, your writing will be slower.',
  button: 'Dismiss',
});
onTravel('rift_valour', {
  shouldAddReminder: true,
  title: 'Valour Rift'
  text: 'You should go to Floating Islands instead.',
  button: 'Travel',
  action: () => { app.pages.TravelPage.travel('floating_islands'); },
});

Data

doRequest

Make a request to a MouseHunt endpoint.

Parameters

NameTypeRequiredDescription
urlstringYesThe URL to make the request to.
dataobjectNoThe data to send with the request.

Examples

doRequest(
  'managers/ajax/pages/page.php',
  {
    page_class: 'HunterProfile',
    'page_arguments[tab]': 'mice',
    'page_arguments[sub_tab]': 'location',
  }
).then((data) => {
  // Do something with the data.
});
const itemData = await doRequest('managers/ajax/users/userInventory.php', {
  action: 'get_items',
  'item_types[]': 'cheddar_cheese'
});

console.log(itemData); // { cheddar_cheese: { ... } }

Page and User Data

getCurrentLocation

Returns the current location.

getCurrentPage

Returns the current page.

getCurrentTab

Returns the current tab.

getCurrentSubtab

Returns the current subtab, falling back to the current tab if there is no subtab.

isOverlayVisible

Returns whether there is currently a popup / overlay visible.

getCurrentOverlay

Returns the current visible popup / overlay.

isDarkMode

Returns whether the user is using dark mode, either via the extension or MHCT dark mode option.

isLoggedIn

Returns whether the user is logged in.

isLegacyHUD

Returns whether the user is using the legacy HUD.

userHasItem

Returns whether the user has a specific item.

userHasItem(itemType);

Parameters

NameTypeRequiredDescription
itemTypestringYesThe type of item to check for.

Examples

if (! userHasItem('super_brie_cheese')) {
  console.log("Oh no! You're out of SB!");
}

getUserItems

Get the data for an array of items.

Parameters

NameTypeRequiredDescription
itemTypesarrayYesThe types of items to get data for.

Examples

const items = await getUserItems(['super_brie_cheese']);
console.log(items); // { super_brie_cheese: { ... } }

getUserSetupDetails

Gets the details of the user's setup, including the current power, luck, etc from each component and aura.

getUserSetupDetails()

Popups

createPopup

Creates a popup with the given title and content. Make sure to add an event listener to the target element to show the popup.

Parameters

NameTypeRequiredDescription
optionsobjectYesThe options for the popup.
options.titlestringNoThe title of the popup.
options.contentstringNoThe content of the popup.
options.hasCloseButtonbooleanNoWhether the popup should have a close button.
options.templatestringNoThe template to use for the popup.
options.showbooleanNoWhether the popup should be shown immediately.
options.classNamestringNoThe class name to add to the popup.

Examples

const examplePopupTarget = document.querySelector('.mousehuntHud-gameInfo');
if (examplePopupTarget) {
  examplePopupTarget.addEventListener('click', () => {
    createPopup({
      title: 'Example Popup',
      content: 'This is an example popup.',
    });
  });
}

createChoicePopup

Creates a popup with two choices.

Parameters

NameTypeRequiredDescription
optionsobjectYesThe options for the popup.
options.titlestringNoThe title of the popup.
options.choicesarrayYesThe choices for the popup.
options.choices.idstringYesThe id of the choice.
options.choices.namestringYesThe name of the choice.
options.choices.imagestringYesThe image of the choice.
options.choices.metastringYesThe subheading of the choice.
options.choices.textstringYesThe text of the choice.
options.choices.buttonstringYesThe text of the button.
options.choices.actionfunctionYesThe callback function for the choice.

Examples

createChoicePopup({
  title: 'Choose your favorite Mouse',
  choices: [
    {
      id: 'treasurer_mouse',
      name: 'Treasurer',
      image: 'https://www.mousehuntgame.com/images/mice/medium/bb55034f6691eb5e3423927e507b5ec9.jpg?cv=2',
      meta: 'Mouse',
      text: 'This is a mouse',
      button: 'Select',
      callback: () => {
        console.log('treasurer selected');
      }
    },
    {
      id: 'high_roller_mouse',
      name: 'High Roller',
      image: 'https://www.mousehuntgame.com/images/mice/medium/3f71c32f9d8da2b2727fc8fd288f7974.jpg?cv=2',
      meta: 'Mouse',
      text: 'This is a mouse',
      button: 'Select',
      callback: () => {
        console.log('high roller selected');
      }
    },
  ],
});

createImagePopup

Creates a popup with the given title and content.

Parameters

NameTypeDescription
optionsobjectYesThe options for the popup.
options.titlestringNoThe title of the popup.
options.imagestringNoThe image to show in the popup.
options.showbooleanNoWhether the popup should be shown immediately.

Examples

const exampleImagePopupTarget = document.querySelector('.mousehuntHud-userStat-row.points');
if (exampleImagePopupTarget) {
  exampleImagePopupTarget.addEventListener('click', () => {
    createImagePopup({
      title: 'Example Image Popup',
      image: 'https://example.com/example.png',
    });
  });
}

createLarryPopup

Create a popup with the Larry's office style.

Parameters

NameTypeDescription
contentstringYesThe content of the popup.

Examples

const exampleLarryPopupTarget = document.querySelector('.mousehuntHud-userStat-row.gold');
if (exampleLarryPopupTarget) {
  exampleLarryPopupTarget.addEventListener('click', () => {
    createLarryPopup('This is an example Larry popup.');
  });
}

createPaperPopup

Create a popup with the paper style.

Parameters

NameTypeDescription
optionsobjectYesThe options for the popup.
options.titlestringNoThe title of the popup.
options.contentobjectNoThe content of the popup.
options.content.titlestringNoThe title of the content.
options.content.textstringNoThe text of the content.
options.content.imagestringNoThe image of the content.
options.content.buttonobjectNoThe button of the content.
options.content.button.textstringNoThe text of the button.
options.content.button.hrefstringNoThe href of the button.
options.showbooleanNoWhether the popup should be shown immediately.

Examples

const examplePaperPopupTarget = document.querySelector('.mousehuntHud-userStat-row.gold');
if (examplePaperPopupTarget) {
  examplePaperPopupTarget.addEventListener('click', () => {
    createPaperPopup({
      title: 'Example Paper Popup',
      content: {
        title: 'Example Paper Popup',
        text: 'This is an example paper popup.',
        image: 'https://example.com/example.png',
        button: {
          text: 'Example Button',
          href: 'https://example.com',
        },
      },
    });
  });
}

createMapPopup

Creates a popup that shows while in the map popup.

Parameters

NameTypeDescription
optionsobjectYesThe options for the popup.
options.titlestringNoThe title of the popup.
options.contentstringNoThe content of the popup.
options.closeClassstringNoThe class to add to the close button.
options.closeTextstringNoThe text to show on the close button.
options.showbooleanNoWhether the popup should be shown immediately.

Examples

const exampleMapPopupTarget = document.querySelector('.mousehuntHud-userStat-row.gold');
if (exampleMapPopupTarget) {
  exampleMapPopupTarget.addEventListener('click', () => {
    createMapPopup({
      title: 'Example Map Popup',
      content: 'This is an example map popup.',
    });
  });
}

createWelcomePopup

Creates a popup that shows once.

Parameters

NameTypeDescription
optionsobjectYesThe options for the popup.
options.idstringYesThe ID of the popup.
options.titlestringNoThe title of the popup.
options.contentstringNoThe content of the popup.
options.columnsarrayNoThe columns of the popup.
options.columns.titlestringNoThe title of the column.
options.columns.contentstringNoThe content of the column.

Examples

const exampleWelcomePopupTarget = document.querySelector('.mousehuntHud-userStat-row.gold');
if (exampleWelcomePopupTarget) {
  exampleWelcomePopupTarget.addEventListener('click', () => {
    createWelcomePopup({
      id: 'example-welcome-popup',
      title: 'Example Welcome Popup',
      content: 'This is an example welcome popup.',
      columns: [
        {
          title: 'Example Column 1',
          content: 'This is an example column 1.',
        },
        {
          title: 'Example Column 2',
          content: 'This is an example column 2.',
        },
      ],
    });
  });
}

showHornMessage

Show a message in the horn popup.

Parameters

NameTypeRequiredDescription
optionsobjectYesThe options for the message.
options.titlestringNoThe title of the message.
options.textstringNoThe text of the message.
options.buttonobjectNoThe button text for the message.
options.actionfunctionNoThe action to perform when the button is clicked.

Examples

showHornMessage({
  title: 'Example Message',
  text: 'This is an example message.',
  button: 'Example Button',
  action: () => {
    console.log('Example action');
  },
});

Settings

addSetting

Adds a boolean setting to the Game Settings page under 'Userscript Settings'.

Parameters

NameTypeRequiredDescription
namestringYesThe name of the setting.
keystringYesThe key of the setting.
defaultValuebooleanNoThe default value of the setting, defaults to true if not provided.
descriptionstringNoThe description of the setting.
sectionobjectNoThe section to add the setting to.
section.idstringNoThe ID of the section to add the setting to.
section.namestringNoThe name of the section to add the setting to.
tabstringNoThe tab to add the setting to. Must be added with addSettingTab() first.

Examples

addSetting('Enable Turbo Mode', 'turbo-mode-enabled', true, 'Enables turbo mode.');
addSetting(
  'My Setting',
  'my-fancy-setting-key',
  false,
  'This is my fancy setting that even has a special section.',
  {
    id: 'my-fancy-section',
    name: 'My Fancy Section',
  }
);

addSetting(
  'My Other Setting',
  'my-other-fancy-setting-key',
  false,
  'This is my other fancy setting that goes in the same section.',
  {
    id: 'my-fancy-section',
    name: 'My Fancy Section',
  }
);

addSettingsTab

Adds a tab to the Game Settings page to show a custom settings page.

Parameters

NameTypeRequiredDescription
identifierstringYesThe identifier of the tab.
namestringYesThe name of the tab.

Examples

addSettingsTab('my-tab', 'My Tab');
const settingSection = {
  id: 'my-section',
  name: 'My fancy section',
};

const tab = addSettingsTab('my-tab', 'My Fancy Tab');

addSetting(
  'This is my fancy setting',
  'my-fancy-setting-key',
  false,
  'This is my fancy setting that goes in the fancy section.',
  settingSection,
  tab
);

getSetting

Gets the value of a setting.

Examples

const mySetting = getSetting('setting_key');
if (mySetting) {
  // Do something.
} else {
  // Do something else.
}

saveSetting

Saves a setting.

Parameters

NameTypeRequiredDescription
keystringYesThe key of the setting.
valuebooleanYesThe value of the setting.
identifierstringNoThe identifier of the setting group.

Examples

saveSetting('setting_key', true);

createFavoriteButton

Creates a favorite button that can be toggled on and off.

Parameters

NameTypeRequiredDescription
optionsobjectYesThe options for the favorite button.
options.idstringYesThe ID of the favorite button.
options.targetNodeYesThe element to append the favorite button to.
options.sizestringNoThe size of the favorite button, defaults to small.
options.defaultStatebooleanNoThe default state of the favorite button, defaults to false.

Examples

const infobar = document.querySelector('.mousehuntHud-gameInfo');

createFavoriteButton({
  id: 'testing_favorite',
  target: infobar,
  size: 'small',
  defaultState: false,
});

Miscellaneous

addStyles

Add CSS styles to the page.

Parameters

NameTypeRequiredDescription
stylesstringYesThe CSS styles to add.
idstringNoThe ID of the style element.
displayOncebooleanNoIf enabled, the styles will only be added if that id has not been used before.

Examples

addStyles(`
  .my-class {
    color: red;
  }

  .huntersHornView {
    display: none;
  }
`);

addSubmenuItem

Add a submenu item to the menu.

Parameters

NameTypeRequiredDescription
optionsobjectYesThe options for the submenu item.
options.menustringNoThe menu to add the submenu item to, defaults to Kingdom.
options.labelstringNoThe label of the submenu item.
options.iconstringNoThe icon of the submenu item, defaults to the stars icon.
options.hrefstringNoThe link for the submenu item.
options.classstringNoThe class of the submenu item.
options.callbackfunctionNoThe callback function to run when the submenu item is clicked.
options.externalbooleanNoWhether the submenu item should show the external link icon, defaults to false.

Examples

addSubmenuItem({
  menu: 'kingdom',
  label: 'mouse.rip',
  icon: 'https://www.mousehuntgame.com/images/ui/hud/menu/prize_shoppe.png',
  href: 'https://mouse.rip',
  external: true,
});

addItemToGameInfoBar

Adds a link to the top 'Hunters Online' bar.

Parameters

NameTypeRequiredDescription
optionsobjectYesThe options for the submenu item.
options.labelstringNoThe label of the submenu item.
options.iconstringNoThe icon of the submenu item, defaults to the stars icon.
options.hrefstringNoThe link for the submenu item.
options.classstringNoThe class of the submenu item.
options.callbackfunctionNoThe callback function to run when the submenu item is clicked.
options.externalbooleanNoWhether the submenu item should show the external link icon, defaults to false.

Examples

addItemToGameInfoBar({
  label: 'mouse.rip',
  icon: 'https://www.mousehuntgame.com/images/ui/hud/menu/prize_shoppe.png',
  href: 'https://mouse.rip',
  external: true,
});
addItemToGameInfoBar({
  label: 'Spring Egg Hunt Helper',
  icon: 'https://www.mousehuntgame.com/images/items/convertibles/transparent_thumb/887162c61d0e01985ffc12e41c03d952.png?cv=2',
  class: 'mh-egg-helper-top-menu',
  callback: bookPopup
});

makeElement

Create an element with a given class and text, optionally appending it to another element.

Parameters

NameTypeRequiredDescription
typestringYesThe type of element to create.
classNamestringNoThe class name to add to the element.
textstringNoThe text to add to the element.
appendToNodeNoThe element to append the new element to.

Examples

const myElement = makeElement('div', 'my-class', 'My Text');
makeElement('a', 'some-class', 'This is a link', document.body);

makeButton

Creates a button to match the button style.

Parameters

NameTypeRequiredDescription
textstringYesThe text of the button.
hrefstringYesThe href of the button.
tinybooleanNoWhether the button should be tiny.
extraClassesarrayNoAny extra classes to add to the button.

Examples

const myButton = makeButton('My Button', 'https://example.com');

makeElementDraggable

Make an element draggable, with the ability to automatical saving and restoring the position.

Parameters

NameTypeRequiredDescription
dragTargetstringYesThe selector for the element to make draggable.
dragHandlestringYesThe selector for the element to use as the drag handle.
defaultXnumberNoThe default X position of the element, defaults to 0.
defaultYnumberNoThe default Y position of the element, defaults to 0.
storageKeystringNoThe key to use for storing the position in localStorage, defaults to dragTarget.
savePositionbooleanNoWhether the position should be saved in localStorage, defaults to true.

Examples

makeElementDraggable(
  '#mh-catch-stats',
  '.mh-catch-stats-header',
  25,
  25,
  'mh-catch-stats-position'
  true
);

wait

Wait for a number of seconds before resolving a promise.

Parameters

NameTypeRequiredDescription
msnumberYesThe number of milliseconds to wait.

Examples

wait(1000).then(() => {
  console.log('1 second has passed');
});

Debugging

clog

Log a message to the console.

Parameters

NameTypeRequiredDescription
messagestring|objectYesThe message to log to the console.

Examples

clog('Hello World');
clog('hello', 'world');

debug

Log a message to the console if debugging is enabled.

Parameters

NameTypeRequiredDescription
messagestring|objectYesThe message to log to the console.

Examples

const myVar = getSomeVar();
debug(myVar);

enableDebugMode

Adds an option in the preferences to enable debugging, must be called before using debug.

1.10.5

8 months ago

1.10.4

8 months ago

1.10.3

8 months ago

1.10.2

8 months ago

1.9.0

8 months ago

1.8.7

8 months ago

1.8.6

8 months ago

1.8.5

8 months ago

1.8.4

8 months ago

1.8.3

8 months ago

1.11.0

7 months ago

1.10.1

8 months ago

1.10.0

8 months ago

1.8.2

8 months ago

1.8.1

8 months ago

1.8.0

9 months ago

1.7.3

10 months ago

1.7.0

10 months ago

1.7.5

10 months ago

1.7.4

10 months ago

1.6.4

1 year ago

1.5.5

1 year ago

1.6.3

1 year ago

1.5.4

1 year ago

1.5.3

1 year ago

1.6.1

1 year ago

1.5.2

1 year ago

1.6.0

1 year ago

1.5.1

1 year ago

1.5.0

1 year ago

1.4.2

1 year ago

1.4.1

1 year ago

1.4.0

1 year ago

1.3.0

1 year ago

1.2.0

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago