Greasy Fork

Greasy Fork is available in English.

FR:ES - Viewer

Show all QCs in TaoBao and Yupoo

目前为 2021-05-31 提交的版本。查看 最新版本

// ==UserScript==
// @name         FR:ES - Viewer
// @namespace    https://www.reddit.com/user/RobotOilInc
// @version      0.3.2
// @description  Show all QCs in TaoBao and Yupoo
// @author       RobotOilInc
// @match        http://*/*
// @grant        none
// @license      MIT
// @homepageURL  https://www.qc-server.cf/
// @supportURL   http://greasyfork.icu/en/scripts/426976-fr-es-viewer
// @include      /^https?://((?:item|2)\.taobao|detail\.tmall)\.com/(item|meal_detail)\.(htm|html)\?/
// @include      /^https?://world.(?:taobao|tmall).com/item/\d+.htm/
// @include      /^https?://world.(?:taobao|tmall).com/item/\d+.html/
// @include      /^https?://.*\.x\.yupoo\.com/albums/\d+/
// @require      https://unpkg.com/[email protected]/src/logger.min.js
// @require      https://unpkg.com/[email protected]/dist/jquery.min.js
// @require      https://unpkg.com/[email protected]/dist/swagger-client.browser.min.js
// @require      https://unpkg.com/[email protected]/js/iframeResizer.min.js
// @run-at       document-end
// ==/UserScript==

const _SWAGGER_DOC_URL = 'https://www.qc-server.cf/api/doc.json';
const _DOMAINS = ['https://localhost:8000', 'https://www.qc-server.cf'];
const _VERSION = GM_info.script.version;

// eslint-disable-next-line func-names
(async function () {
  // Setup the logger.
  Logger.useDefaults();

  // Log the start of the script.
  Logger.info(`Starting extension, version ${_VERSION}`);

  /** @type {SwaggerClient} */
  let client;

  // Try to create Swagger client from our own documentation
  try {
    client = await new SwaggerClient({ url: _SWAGGER_DOC_URL });
  } catch (error) {
    Logger.error(`We are unable to connect to FR:ES: ${_SWAGGER_DOC_URL}`, error);

    return;
  }

  const $iframe = $('<iframe id="qciframe" style="border:0" />');
  $iframe.on('load', () => {
    $iframe.iFrameResize({ checkOrigin: _DOMAINS, bodyMargin: 10, bodyPadding: 10 });
    $iframe.show();
  });

  // Are we on a Yupoo album?
  if (window.location.hostname.includes('yupoo.com')) {
    const id = window.location.href.match(/^https?:\/\/.*\.x\.yupoo\.com\/albums\/(\d+)/)[1];
    const author = window.location.hostname.replace('.x.yupoo.com', '');

    // Build URL
    const request = SwaggerClient.buildRequest({
      spec: client.spec,
      operationId: 'viewYupoo',
      parameters: { id, author },
      responseContentType: 'application/json',
    });

    // Hide, to later show and add the src
    $iframe.hide().attr('src', request.url);

    // Finally append
    $('.showalbum__imagecardwrap').append($iframe);
    return;
  }

  // Are we on a Taobao/Tmall item page?
  if (window.location.hostname.includes('taobao') || window.location.hostname.includes('tmall')) {
    const searchParams = new URLSearchParams(window.location.search);
    const id = searchParams.get('id');

    // Build URL
    const request = SwaggerClient.buildRequest({
      spec: client.spec,
      operationId: 'viewTaobao',
      parameters: { id },
      responseContentType: 'application/json',
    });

    // Hide, to later show and add the src
    $iframe.hide().css('width', '770px').attr('src', request.url);

    // Finally prepend
    $('#description').prepend($iframe);

    return;
  }

  Logger.debug('Unsupported website/missing if statement');
}());