Toggle DLsite Language

Switch between english and japanese language in DLsite

目前為 2021-04-06 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Toggle DLsite Language
// @namespace    zero.toogledlsitelan
// @description  Switch between english and japanese language in DLsite
// @include      *://www.dlsite.com/*
// @version      1.4
// @grant        none
// @project page https://greasyfork.org/en/scripts/402452-toggle-dlsite-language
// ==/UserScript==

(function () {
  var url = window.location.href;
  var cookies = document.cookie.split(';');
  var locale = '';

  // Get locale from cookies
  cookies.forEach(element => {
    if (element.includes('locale'))
      locale = element.substring(element.indexOf('=') + 1);
  });

  // If locale isn't found finish script and give error
  if (locale === '') {
    console.log("DLsite Toogle couldn't find locale cookie, stopping script");
    return false;
  }

  // Add toggle button
  if (url.includes('maniax')) {
    var navlink = document.getElementsByClassName('floorNavLink');
    let enButton = '<div class="floorNavLink-item type-general" style="width:125px"><a style="font-weight:bold"' +
                   'id="toggle">Toggle Site (EN)</a></div>';
    let jpButton = '<div class="floorNavLink-item type-general" style="width:125px"><a style="font-weight:bold"' +
                   'id="toggle">Toggle Site (JP)</a></div>';

    if (url.includes('?locale=')) {
      if (url.includes('ja_JP')) {
        navlink[0].innerHTML =  enButton + navlink[0].innerHTML;
      } else {
        navlink[0].innerHTML = jpButton + navlink[0].innerHTML;
      }
    } else {
      if (locale === 'ja-jp') {
        navlink[0].innerHTML = enButton + navlink[0].innerHTML;
      } else {
        navlink[0].innerHTML = jpButton + navlink[0].innerHTML;
      }
    }
  }

  // Create link on toggle button once rest of document is loaded
  document.addEventListener('DOMContentLoaded', function () {
    var toggle = document.getElementById('toggle');

    if (url.includes('?locale=')) {
      if (url.includes('ja_JP')) url = url.replace('ja_JP', 'en_US');
      else url = url.replace('en_US', 'ja_JP');
    } else {
      if (locale === 'ja-jp') url += '?locale=en_US';
      else url += '?locale=ja_JP';
    }

    toggle.href = url;
  });
})();