Custom gesture examples - utubo/firefox-simple_gesture Wiki

Tab

Input keyword and open google in a new tab

/**
 * @name google
 */
const q = prompt();
if (q) {
  SimpleGesture.open('https://www.google.com/search?q=' + encodeURIComponent(q));
}

Open target link in background

/**
 * @name Open in bg
 */
let target = SimpleGesture.target;
while (target && !target.href) {
  target = target.parentNode;
}
if (target) {
  SimpleGesture.open(target.href, { active: false });
}

Show URL and open target link in current tab.

/**
 * @name Confirm URL
 */ 
for (let target = SimpleGesture.target; !!target; target = target.parentNode) {
  if (target.href) {
    if (confirm('Are you sure you want to open this URL?\n' + target.href)) {
      location.href = target.href;
    }
    break;
  }
}

Duplicate current tab. (Webextentions can't duplacted session.)

/**
 * @name Duplicate current tab
 */ 
SimpleGesture.open(location.href);

Common

Paste and Go

/**
 * @name Paste and Go
 */
navigator.clipboard.readText().then(text => {
  if (text.startsWith("http://") || text.startsWith("https://")) {
    SimpleGesture.open(text);
  } else {
    SimpleGesture.open('https://www.google.com/search?q=' + encodeURIComponent(text));
  }
});

Focus to the first inputbox

/** @name Focus to the first inputbox */
for (let e of document.getElementsByTagName('INPUT')) {
  if (!e.getAttribute('type') || e.getAttribute('type').match(/text|search/)) {
    e.focus();
    e.select();
    break;
  }
}

Add a margin at the top of the HTML

This script adds a margin at the top of the HTML to make it easier to tap. Tapping on the added margin will restore it.

/** @name Margin-top */
if (window._SGMT_) {
  _SGMT_.apply();
} else {
  window._SGMT_ = {
    HEIGHT: '60vh',
    COLOR: '#d7d7db',
    apply: () => {
      document.documentElement.classList.add('SGMT');
      scrollTo({top:0, behavior:'smooth'});
    },
    reset: e => {
      if (e.target.tagName!='HTML') return;
      document.documentElement.classList.remove('SGMT');
    },
  };
  const s = document.createElement('style');
  s.textContent = `
    html::before {
      background:${_SGMT_.COLOR};
      height:0vh;
      display:block;
      transition:height .5s;
      content:"";
    }
    html.SGMT::before {
      height:${_SGMT_.HEIGHT};
    }
    .SGMT .SGMT-FIXED {
      top:${_SGMT_.HEIGHT};
    }
  `;
  s.type = 'text/css';
  document.head.appendChild(s);
  let f = document.elementFromPoint(0, 0);
  while (f) {
    if (f.tagName === 'BODY' || f.tagName === 'HTML') break;
    if (getComputedStyle(f).getPropertyValue('position') === 'fixed') {
      f.classList.add('SGMT-FIXED');
      break;
    }
    f = f.parentNode;
  }
  addEventListener('click', _SGMT_.reset);
  setTimeout(_SGMT_.apply, 10);
}

Media

Cancel Fullscreen (only media player)

/** @name Cancel fullscreen */
document.mozFullScreenElement && document.mozCancelFullScreen();

Sorry, I couldn't find the way to go fullscreen.

Rewind / Seek forward a video

Rewind 5sec.

/** @name Rewind a video */
let video = SimpleGesture.target;
if (!video || video.tagName != 'VIDEO') {
  video = document.getElementsByTagName('VIDEO')[0];
}
if (video) {
  video.currentTime = video.currentTime - 5;
}

Seek forward 5sec.

/** @name Seek forward a video */
let video = SimpleGesture.target;
if (!video || video.tagName != 'VIDEO') {
  video = document.getElementsByTagName('VIDEO')[0];
}
if (video) {
  video.currentTime = video.currentTime + 5;
}

You can insert this to first line for work on only Youtube.

if (!location.href.startsWith('https://m.youtube.com/')) { SimpleGesture.exit(); }

[jp]