') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); })(); GitHub - 9technology/inquisitive: Inquirer middleware engine. · GitHub
Skip to content

Repository files navigation

inquisitive

Build StatusCoverage Status

Inquirer middleware engine. Create interactive cli prompts with a middleware engine for handling individual answers.

Example

import'@babel/register';// enable async/awaitimportinquisitivefrom'inquisitive';importdelayfrom'delay';constinq=inquisitive();// define a prompt with a middleware handlerinq.use((prompt)=>{// ask a questionprompt({name: 'name',message: 'What is your name',});// handle the answersreturnasync(answers,status,next)=>{if(answers.name!=='Nicolas Cage'){thrownewError('Legends only');}awaitdelay(1000);awaitnext();// go to next handler};});inq.run();// start asking

Installation

NPM

npm install --save inquisitive

Yarn

yarn add inquisitive

Setup

Inquisitive uses async/await, until this is available for production it is recommended to transpile with Babel.

Use the transform plugin, babel-plugin-transform-async-to-generator.

Usage

Middleware

The concept of middleware in inquisitive is slightly different than common patterns like in Express or Koa. A middleware function is invoked immediately.

Middleware functions have two jobs, ask questions and handle answers. Both of which are optional.

Questions

Middleware functions will be given a prompt function as it's only argument. This prompt function should be called to add questions to inquirer.

inq.use((prompt)=>{// ask anythingprompt({name: 'first',message: 'What is your first name:',});// and many more...prompt({name: 'last',message: 'What is your last name:',});});

Asking questions is completely optional. In some cases only an answers handler is required as prior middleware may have asked all the questions.

Questions Format

See inquirer Questions.

Answers Handler

Once inquirer has complete the answers it will run through the answer handlers. To give a handler to inquisitive just return an async function from the middleware function.

Answer handler functions take 3 arguments, answers, status and next.

inq.use((prompt)=>{// ask questions?returnasync(answers,status,next)=>{status('checking something');// update spinner textdoSomething(answers.foo);// handle answersawaitnext();// don't forget to move next};});

Handling answers is also optional. Some middleware may just want to ask questions and have another middleware handle the answers.

Running Cli

To run the interactive prompt call #run() on the inquisitive instance. run also takes a few options to control how inquisitive will report to the terminal.

inq.run({args: true,// read cli args and set question default valuesspinner: true,// enable/disable spinnertime: true,// enable/disable time message at end});

Args Defaults

Inquisitive has the ability to read args from the terminal and set them as the defaults for questions. This is a convenience options enabled by default.

cli.js

inq.use((prompt)=>{prompt({name: 'name',message: 'What is your name:',});});inq.run();
node cli.js --name "Nicolas Cage"? What is your name: (Nicolas Cage) _

Custom Inquirer

It is also possible to give a custom built inquirer module to inquisitive. Just pass it into the factory method when creating an instance.

importinquirerfrom'inquirer';importinquisitivefrom'inquisitive';constcustom=inquirer.createPromptModule();// apply customisationsconstinq=inquisitive(custom);

API

inquisitive([module])
  • module: Function Custom inquirer module.

Returns inquisitive instance.

Instance

#use(fn)
  • fn: Function Middleware function to add to instance.
#run([opts])
  • otps: Object Run options.
    • opts.args: Boolean Enable/disable argument defaults.
    • opts.spinner: Boolean Enable/disable spinner.
    • opts.time: Boolean Enable/disable time feedback on success.

Returns Promise, resolving answers.

License

BSD-3-Clause

Copyright (c) 2016 9Technology

About

Inquirer middleware engine.

Resources

Stars

2 stars

Watchers

9 watching

Forks

Releases

Packages

Used by

Contributors

Languages