Greasy Fork

Greasy Fork is available in English.

FR:ES - Viewer

Show all QCs in TaoBao and Yupoo

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

// ==UserScript==
// @name        FR:ES - Viewer
// @namespace   https://www.reddit.com/user/RobotOilInc
// @author      RobotOilInc
// @version     1.1.0
// @description Show all QCs in TaoBao and Yupoo
// @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+/
// @connect     self
// @connect     localhost
// @require     https://code.jquery.com/jquery-3.4.1.min.js
// @require     https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/4.3.1/iframeResizer.js
// @require     http://greasyfork.icu/scripts/426983-gm-fetch/code/GM%20Fetch.js
// @require     https://unpkg.com/swagger-client
// @grant       GM_xmlhttpRequest
// @run-at      document-end
// ==/UserScript==

/* jshint esversion: 8 */
/* globals $: false, SwaggerClient: false */

const _SWAGGER_DOC_URL = 'https://localhost:8000/api/doc.json';
const _DOMAINS = ['https://localhost:8000', 'https://fashionreps.tools'];

// eslint-disable-next-line func-names
(async function () {
  // Create Swagger client from our own documentation
  const client = await new SwaggerClient(_SWAGGER_DOC_URL);

  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;
  }

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