Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ThinkBeforeYouTweet - a Nudge for HabitLab to prompt reflection before tweeting
// Nudge for twitter.com for HabitLab
// You can use it by installing HabitLab for Chrome,
// and then in settings, click Code your own Nudge and paste this code in
// TODOS:
// Make a version for TweetDeck
// Remove nudgeContainer once tweet is successfully sent
const $ = require('jquery');
require_component('habitlab-logo-v2');
require_component('paper-button');
var $nudgeContainer;
var disableInterval;
var questions = [
"Which of your values does this tweet support?",
"In what way might your followers benefit from this tweet?",
"Are you comfortable with condensing this content into a tweet, or is your tweet missing valuable context like your background or bias?",
"Would this content provide significantly more value as a blog post?",
"Would this content be more fitting on other platforms, like Instagram or FB?"
];
function cleanupNudge() {
if ($nudgeContainer) {
$nudgeContainer.find('input').remove();
$nudgeContainer.remove();
}
if (disableInterval) {
window.clearInterval(disableInterval);
}
}
function disableTweetButton() {
// Each input in nudge container must have content
var shouldEnableButton = true;
$('#nudge_container input').each(function(index, element) {
shouldEnableButton = shouldEnableButton && element.value.length > 0;
});
var $tweetButton = $('.tweet-action');
if (shouldEnableButton) {
$tweetButton.removeAttr('disabled');
$tweetButton.show();
} else {
$tweetButton.attr('disabled', 'disabled');
$tweetButton.hide();
}
}
function addNudgeIfOpen($tweetBox) {
if (!$tweetBox.length) {
return;
}
if ($tweetBox.closest('.RichEditor').hasClass('is-fakeFocus') || !$tweetBox.closest('.tweet-form').hasClass('condensed')) {
if (!$nudgeContainer) {
$nudgeContainer = $('<div id="nudge_container"></div>');
questions.forEach((question) => {
var $label = $('<label></label>').text(question);
var $input = $('<input type="text" value=""></input>').css('width', '90%');
$nudgeContainer.append([$label, '<br>', $input, '<br>']);
});
disableInterval = window.setInterval(disableTweetButton, 100);
$('.tweet-form:visible .TweetBoxToolbar').before($nudgeContainer);
disableTweetButton();
}
} else {
cleanupNudge();
}
}
function addNudge() {
var re = new RegExp('twitter.com\/\??.*$');
if (!re.test(window.location.href)) {
return;
}
$('.tweet-box').on('focus', function(e) {
addNudgeIfOpen($(e.target));
});
// Sometimes tweet box starts open
addNudgeIfOpen($('.tweet-form:visible .tweet-box'));
}
addNudge();
window.on_intervention_disabled = () => {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.