Toggle Dlsite language

Switch between english and japanese language in DLsite

Stan na 30-07-2022. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

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

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

    // Get locale from cookies
    cookies.forEach(element => {
      if (/locale=..-../.test(element))
        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;
    }

    // Add toggle button
    var navlink = document.getElementsByClassName('floorNavLink');

    // Window loads two times, stop script on second time
    if(typeof(navlink[0]) === 'undefined') return;

    // Create button node and append it
    let navDiv = document.createElement('div');
    let navA = document.createElement('a');

    navDiv.appendChild(navA);

    navDiv.className = 'floorNavLink-item type-general';
    navDiv.style.width = '125px';
    navA.style.fontWeight = 'bold';

    navlink[0].prepend(navDiv);

    // Set proper text and link to button
    if (url.includes('?locale=')) {
      if (url.includes('ja_JP')) {
        navA.textContent = 'Toggle Site (EN)';
        navA.href = url.replace('ja_JP', 'en_US');
      } else {
        navA.textContent = 'Toggle Site (JP)';
        navA.href = url.replace('en_US', 'ja_JP');
      }
    } else {
      // No locale on url, get from cookies
      
      // Remove version_up at the end of url if it's there
      if(url.includes('#version_up')) url = url.replace('#version_up', '');
        
      if (locale === 'ja-jp') {
        navA.textContent = 'Toggle Site (EN)';
        if(/\/$/.test(url)) navA.href = url + '?locale=en_US'; // Check if url ends with '/'
        else navA.href = url + '/?locale=en_US';
      } else {
        navA.textContent = 'Toggle Site (JP)';
        if(/\/$/.test(url)) navA.href = url + '?locale=ja_JP'; // Check if url ends with '/'
        else navA.href = url + '/?locale=ja_JP';
      }
    }
  });
})();