click a link always open a new tab

click a link always open a new tabs

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         click a link always open a new tab
// @description  click a link always open a new tabs
// @icon         https://img1.baidu.com/it/u=1412332306,99938487&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500
// @license	     MIT
// @version      0.0.2
// @author       lee mathew
// @run-at       document-end
// @include      http*://*
// @namespace https://greasyfork.org/users/1090699
// ==/UserScript==


(function () {


    // const onClick = function (event, href) {
    //     // event.preventDefault();
    //     console.log(event, href)
    // }
    let elements = document.querySelectorAll("a")
    elements.forEach(el => {
        //如果是a标签的href只有javascript:;之类的,需要跳过
        if (el.href.indexOf('javascript:') === -1&&['http','https'].includes(el.href.split(":")[0])) {
            el.target = '_blank'
            // let params = queryURLparamsRegEs6(el.href)
            // params['new_tab_return_path'] = window.location.href
            // el.href = paramsToUrl(params)
            // el.addEventListener('click', (event) => { onClick(event, el.href) })
        }
    })
    // let params = queryURLparamsRegEs6(window.location.href)
    // if (params['new_tab_return_path']) {
    //     console.log('pushState!!!', location.href)
    //     setTimeout(() => {
    //         var thisLocation = location.href;
    //         // history.replaceState({ page: 'intercept', entered: false }, '', intercetpUrl);
    //         history.pushState({ page: 'current' }, '', thisLocation);
    //     }, 1000)

    // }
    // window.addEventListener("popstate", e => {
    //     alert("监听到了浏览器的返回按钮事件");
    // }, false);


    // function queryURLparamsRegEs6(url) {
    //     let obj = {}
    //     obj.baseUrl = url.split("?")[0]
    //     if (url.indexOf('?') < 0) return obj
    //     let arr = url.split('?')
    //     url = arr[1]
    //     let array = url.split('&')
    //     for (let i = 0; i < array.length; i++) {
    //         let arr2 = array[i]
    //         let arr3 = arr2.split('=')
    //         obj[arr3[0]] = arr3[1]
    //     }
    //     return obj
    // }
    // function paramsToUrl(params) {
    //     let ans = params.baseUrl
    //     delete params.baseUrl
    //     Object.keys(params).forEach((key, index) => {
    //         if (index === 0) {
    //             ans += '?'
    //         } else {
    //             ans += '&'
    //         }
    //         ans += `${key}=${params[key]}`
    //     })
    //     return ans
    // }
})();