External link newtaber

this code opens external links in new tab on all sites (now may work on dynamic lists of links such as search results)

目前為 2018-04-24 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name            External link newtaber
// @namespace       almaceleste
// @version         0.2.2
// @description     this code opens external links in new tab on all sites (now may work on dynamic lists of links such as search results)
// @description:ru  этот код открывает внешние ссылки в новой вкладке на всех сайтах (теперь может работать с динамическими списками ссылок, такими как результаты поисковых запросов)
// @author          (ɔ) Paola Captanovska
// @license         AGPL-3.0; http://www.gnu.org/licenses/agpl.txt
// @icon            https://cdn1.iconfinder.com/data/icons/feather-2/24/external-link-32.png
// @icon64          https://cdn1.iconfinder.com/data/icons/feather-2/24/external-link-128.png

// @homepageURL     https://greasyfork.org/en/users/174037-almaceleste
// @homepageURL     https://openuserjs.org/users/almaceleste
// @homepageURL     https://github.com/almaceleste/userscripts
// @supportURL      https://github.com/almaceleste/userscripts/issues

// @run-at          document-start
// @require         https://openuserjs.org/src/libs/sizzle/GM_config.js
// @grant           GM_getValue
// @grant           GM_setValue
// @grant           GM_registerMenuCommand
// @grant           GM_openInTab

// @match           http*://*/*
// ==/UserScript==

// ==OpenUserJS==
// @author almaceleste
// ==/OpenUserJS==

const windowcss = '#allnewtaberCfg {background-color: lightblue;} #allnewtaberCfg .reset_holder {float: left; position: relative; bottom: -1em;} #allnewtaberCfg .saveclose_buttons {margin: .7em;}';
const iframecss = 'height: 30.1em; width: 30em; border: 1px solid; border-radius: 3px; position: fixed; z-index: 999;';

var host = window.location.hostname;
var flat = host.replace(/\..*/, '');
var root = host.replace(/^[^.]*\./, '');
var child = '*.' + host;
var next = '*.' + root;

GM_registerMenuCommand('External link newtaber Settings', opencfg);

function opencfg()
{
	GM_config.open();
	allnewtaberCfg.style = iframecss;
}

GM_config.init(
{
    id: 'allnewtaberCfg',
    title: 'External link newtaber',
    fields:
    {
        level:
        {
            section: ['Exclusions', 'Exclude these domains (do not open in new tab)'],
            label: 'do not exclude parent and neighbor sites if parent site is a root domain like .com',
            labelPos: 'right',
            type: 'checkbox',
            default: true,
        },
        root:
        {
            label: 'parent site links (' + root + ')',
            labelPos: 'right',
            type: 'checkbox',
            default: true,
        },
        next:
        {
            label: 'neighbor site links (' + next + ')',
            labelPos: 'right',
            type: 'checkbox',
            default: true,
        },
        host:
        {
            label: 'this site links (' + host + ')',
            labelPos: 'right',
            type: 'checkbox',
            default: true,
        },
        child:
        {
            label: 'child site links (' + child + ')',
            labelPos: 'right',
            type: 'checkbox',
            default: true,
        },
        background:
        {
            section: ['Options', 'Other options'],
            label: 'open new tab in background',
            labelPos: 'right',
            type: 'checkbox',
            default: false,
        },
        insert:
        {
            label: 'insert new tab next to the current instead of the right end',
            labelPos: 'right',
            type: 'checkbox',
            default: true,
        },
        setParent:
        {
            label: 'return to the current tab after new tab closed',
            labelPos: 'right',
            type: 'checkbox',
            default: true,
        },
    },
    css: windowcss,
    events:
    {
        save: function() {
            GM_config.close();
        }
    },
});

(function() {
    'use strict';

    var empty = new RegExp('^$');
    var patternroot = empty;
    var patternhost = empty;
    host = host.replace(/\./g, '\\\.');
    root = root.replace(/\./g, '\\\.');
    var background = GM_config.get('background');
    var options = {active: !GM_config.get('background'), insert: GM_config.get('insert'), setParent: GM_config.get('setParent')};

    if (GM_config.get('root')){patternroot = new RegExp('^' + root + '$');}    // abc.x               => ^abc\.x$
    if (GM_config.get('next')){
        if (GM_config.get('root')){
            patternroot = new RegExp('[^(' + flat + '\.)]?' + root + '$');     // abc.x + *.abc.x     => [^(w\.)]?abc\.x$
        }
        else {patternroot = new RegExp('[^(' + flat + ')]?\.' + root + '$');}  // *.abc.x             => [^(w)]?\.abc\.x$
    }
    if (GM_config.get('level') && root.search(/\..+\./) == -1){patternroot = empty;}
    if (GM_config.get('host')){patternhost = new RegExp('^' + host + '$');}    // w.abc.x             => ^w\.abc\.x$
    if (GM_config.get('child')){
        if (GM_config.get('host')){
            patternhost = new RegExp('(.+\.)?' + host + '$');                  // w.abc.x + *.w.abc.x => (.+\.)?w\.abc\.x$
        }
        else {patternhost = new RegExp('.+\.' + host + '$');}                  // *.w.abc.x           => .+\.w\.abc\.x$
    }

    window.onload = function(){
        var anchors= document.getElementsByTagName('a');

        for (var i = 0; i < anchors.length; i++) {
            var a = anchors[i];
            var target = a.host;
            if (a.hasAttribute('href')){
                if (target && !empty.test(target)){
                    if (!patternroot.test(target) && !patternhost.test(target)){
                        // a.target = '_blank';
                        a.addEventListener('click', newtaber);
                    }
                }
            }
        }
    };

    function newtaber(e){
        e.preventDefault();
        e.stopPropagation();
        GM_openInTab(this.href, options);
    }
})();