FR:ES - Viewer

Show all QCs in TaoBao and Yupoo

目前為 2021-05-29 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         FR:ES - Viewer
// @namespace    https://www.reddit.com/user/RobotOilInc
// @version      0.1.8
// @description  Show all QCs in TaoBao and Yupoo
// @author       RobotOilInc
// @match        http://*/*
// @grant        none
// @license      MIT
// @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/@sentry/[email protected]/build/bundle.min.js
// @require      https://unpkg.com/@sentry/[email protected]/build/bundle.tracing.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://localhost:8000/api/doc.json';
const _DOMAINS = ['https://localhost:8000', 'https://fashionreps.tools'];
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}`);

  // Setup Sentry for proper error logging, which will make my lives 20x easier, use XHR since fetch dies.
  Sentry.init({
    dsn: 'https://[email protected]/5791114',
    integrations: [new Sentry.Integrations.BrowserTracing()],
    transport: Sentry.Transports.XHRTransport,
    release: `viewer-${_VERSION}`,
    environment: 'production',
  });

  /** @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);
    Sentry.captureException(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;
  }

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