Gab: Quick Share

Adds quick share button to each gab post. Clicking results in copying text with a link to that post into clipboard.

目前為 2019-05-10 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name     Gab: Quick Share
// @version  1
// @grant    none
// @require  https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js
// @match    https://*.gab.com/*
// @match    https://*.gab.ai/*
// @author   monnef
// @description Adds quick share button to each gab post. Clicking results in copying text with a link to that post into clipboard.
// @namespace   monnef.eu
// ==/UserScript==

const debug = false;

const tagCl = 'gqs';
const qsCl = tagCl + '-qs';
const lTag = '[GQS]';

const dLog = (...x) => { if (debug) console.debug(lTag, ...x); };
const log = (...x) => { console.log(lTag, ...x); }
const logErr = (...x) => { console.error(lTag, ...x); }

const addStyle = (style) => {
    style = style instanceof Array ? style.join('\n') : style;
    $('head').append($('<style type="text/css">' + style + '</style>'));
}

addStyle(`
.${qsCl} {
  float: right;
  margin-right: 2em;
  cursor: pointer;
}
`);

const qsText = 'QS';

const qsFlashText = (el, text) => {
  el.text(text);
  setTimeout(() => el.text(qsText), 500);
};

const extractTextForQS = (el) => {
  let innerEls = el.find('.gab__body div p');
  if (!innerEls.length) innerEls = el.find('.gab__body div'); // in case of no new lines
  const text = innerEls.map((i, x) => {
    const el = $(x);
    dLog('1, i=', i, 'x=', x, 'el=', el);
    return el.contents().map((j, y) => {
      dLog('2, j=', j, 'y=', y);
      if (y.nodeType === Node.TEXT_NODE) return y.nodeValue;
      const el2 = $(y);
      dLog('2. el2=', el2);
      if (y.tagName === 'A') { 
        if (el2.hasClass('inner-post-hashtag')
            || el2.hasClass('inner-post-mention')
           ) {
          return el2.text();
        };
        return el2.attr('href');
      };
      return el2.text();
    }).toArray().concat('\n');
  }).toArray().join('');
  return text;
};

const handleQuickShare = (el) => {
  const qsEl = el.find(`.${qsCl}`);
  const postLinks = el.find('.gab__meta__info span a').filter((i, x) => x.href.includes('/posts/'));
  if (postLinks.length != 1) {
    logErr('failed to locate a post link', postLinks);
  } else {
    const text = extractTextForQS(el);
    const link = `https://gab.com${postLinks.attr('href')}`;
    const toCopy = `${text}\n${link}`;
    dLog({link, text, toCopy});
    log('copying to clipboard:\n' + toCopy);
    navigator.clipboard.writeText(toCopy)
      .then(() => {
        log('copy ok');
        qsFlashText(qsEl, '✅');
      })
      .catch(e => {
        logErr('copy failed', e);
        // alert(`Copying to clipboard failed: ${e}`);
        qsFlashText(qsEl, '❎');
      });
  }
};

const processOneGab = (el) => {
  const qsEl = $('<a/>').text(qsText).addClass(qsCl).attr('title', 'Quick Share by monnef').click(() => handleQuickShare(el));
  el.find('.gab__meta__author').css('display', 'inline-block').after(qsEl);
};

const work = () => {
  $('div.gab').each((idx, rEl) => {
    const el = $(rEl);
    if(el.hasClass(tagCl)) return;
    el.addClass(tagCl);
    processOneGab(el);
  });
  setTimeout(work, 500);
}

$(() => {
  const suf = ' background-color: black; font-size: 150%;';
  console.info('%c' + lTag + ' 🐸👌 %cGab: Quick Share %cby 🔸%cmonnef🔸%c [🍍]',
    'color: gray;' + suf,
    'color: lime;'+ suf,
    'color: white;' + suf,
    'color: magenta;' + suf,
    'color: gray;' + suf,
  );
  work();
});