Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export modbarExists promise #617

Merged
merged 2 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions extension/data/modules/modbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ import * as TBHelpers from '../tbhelpers.js';
import * as TBCore from '../tbcore.js';
import * as TBApi from '../tbapi.js';

// Hold onto the modbarExists resolver so we can call it when the time is right
let resolveModbarExists = null;

/**
* A promise which resolves when the modbar is added to the page.
* @constant {Promise<void>}
*/
export const modbarExists = new Promise(resolve => {
resolveModbarExists = resolve;
});

export default new Module({
name: 'Modbar',
id: 'Modbar',
Expand Down Expand Up @@ -306,6 +317,9 @@ export default new Module({

toggleMenuBar(modbarHidden);

// modbar was added to the DOM, let everyone know so they can add buttons and stuff
resolveModbarExists();

// moderated subreddits button.
if (enableModSubs) {
TBCore.getModSubs().then(async () => {
Expand Down
8 changes: 6 additions & 2 deletions extension/data/modules/personalnotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import * as TBui from '../tbui.js';
import * as TBHelpers from '../tbhelpers.js';
import * as TBCore from '../tbcore.js';

import {modbarExists} from './modbar.js';

export default new Module({
name: 'Personal Notes',
id: 'PNotes',
Expand Down Expand Up @@ -124,8 +126,10 @@ export default new Module({
});
};

// add the button to the modbar
$body.find('#tb-toolbarshortcuts').before(' <a href="javascript:void(0)" class="tb-modbar-button" id="tb-personal-notes-button">Personal Notes</a>');
// add the button to the modbar once the modbar is available
modbarExists.then(() => {
$body.find('#tb-toolbarshortcuts').before(' <a href="javascript:void(0)" class="tb-modbar-button" id="tb-personal-notes-button">Personal Notes</a>');
});

// Since we have a button we can click on it!
$body.on('click', '#tb-personal-notes-button', async () => {
Expand Down