Lastest Doc Redirector (python, django, ruby)

Redirect the version you googled to the latest doc versions.

Από την 10/02/2017. Δείτε την τελευταία έκδοση.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         Lastest Doc Redirector (python, django, ruby)
// @name:vi      Chuyển trang tài liệu mới nhất (python, django, ruby)
// @namespace    https://github.com/hotmit/latest-doc-redirector-userscript
// @version      1.0.0
// @description  Redirect the version you googled to the latest doc versions.
// @description:vi  Chuyển những trang bạn tìm được qua trang tài liệu với phiên bản mới nhất (python, django, ruby)
// @author       Du Dang
// @include https://docs.djangoproject.com/*
// @include https://docs.python.org/2/*
// @include https://ruby-doc.org/core-*
// @grant GM_getValue
// @grant GM_setValue
// @license      MIT
// ==/UserScript==
(function() {
    'use strict';

    // Change django version number
    var DJANGO_TARGET_VERSION = '1.11';
    var RUBY_TARGET_VERSION = '2.4.0';
    var PYTHON_TARGET_VERSION = '3';
	
    var Str = {};

    $(function(){
		var href = window.location.href;

		function forward(url){
			var lastFwd = GM_getValue('LastUrlFwd', '');
			// do this to allow browser to go back to old version 
			// 		without loop redirect
			if (lastFwd != url){
				GM_setValue('LastUrlFwd', url);
				window.location.href = url;
			}
		}

		if (Str.contains(href, 'docs.djangoproject.com') 
			&& !Str.contains(href, '/en/' + DJANGO_TARGET_VERSION + '/'))
		{
			// https://docs.djangoproject.com/en/1.5/topics/db/optimization/
			// INTO https://docs.djangoproject.com/en/1.7/topics/db/optimization/
			href = href.replace(/docs.djangoproject.com\/(\w{2})\/(\d\.\d|dev)/gi, 
					'docs.djangoproject.com/en/' + DJANGO_TARGET_VERSION);
			forward(href);
		}

		else if (Str.contains(href, 'docs.python.org') 
			&& !Str.contains(href, '/' + PYTHON_TARGET_VERSION + '/'))
		{
			// https://docs.python.org/2/library/datetime.html
			// INTO https://docs.python.org/3/library/datetime.html
			href = href.replace(/docs.python.org\/2\/library/gi, 
					'docs.python.org/' + PYTHON_TARGET_VERSION + '/library');
			forward(href);
		}

		else if (Str.contains(href, 'ruby-doc.org/core-') 
			&& !Str.contains(href, '/core-' + RUBY_TARGET_VERSION + '/'))
		{
			// https://ruby-doc.org/core-2.2.0/String.html
			// INTO https://ruby-doc.org/core-2.3.1/String.html
			href = href.replace(/core-[^\/]+/gi, 'core-' + RUBY_TARGET_VERSION);
			forward(href);
		}
		
    }); //end jqReady

    // region [ Str Lib ]
	Str.contains = function(s, needle, caseSensitive) {        
        if (caseSensitive){
            return s.indexOf(needle) > -1;
        }
        return s.toLowerCase().indexOf(needle.toLowerCase()) > -1;
    };
    // endregion

})();