Skip to content

Instantly share code, notes, and snippets.

@ciknight
Forked from ambar/nospm.user.js
Created August 10, 2017 03:08
Show Gist options
  • Select an option

  • Save ciknight/7aca1886c9ca77bfaa1f4afd99bb2164 to your computer and use it in GitHub Desktop.

Select an option

Save ciknight/7aca1886c9ca77bfaa1f4afd99bb2164 to your computer and use it in GitHub Desktop.

Revisions

  1. @ambar ambar revised this gist Feb 3, 2017. No changes.
  2. @ambar ambar revised this gist Sep 13, 2016. No changes.
  3. @ambar ambar revised this gist Sep 13, 2016. 1 changed file with 25 additions and 24 deletions.
    49 changes: 25 additions & 24 deletions nospm.user.js
    Original file line number Diff line number Diff line change
    @@ -1,62 +1,63 @@
    // ==UserScript==
    // @name nospm
    // @version 1.1
    // @description 移除虾米、淘宝和天猫网址中的 spm 参数(包括地址栏和页面中的链接)
    // @include http://*.xiami.com/*
    // @include http://*.taobao.com/*
    // @include http://*.tmall.com/*
    // @include *://*.xiami.com/*
    // @include *://*.taobao.com/*
    // @include *://*.tmall.com/*
    // ==/UserScript==

    var each = [].forEach
    let forEach = Function.call.bind([].forEach)

    var removeSpm = function(url, next) {
    var search = url.search
    let removeSpm = (url, next) => {
    let search = url.search
    if (!search) return

    var query = parseQuery(search.slice(1))
    let query = parseQuery(search.slice(1))
    if (query.spm) {
    delete query.spm
    var param = toParam(query)
    let param = toParam(query)
    next(url.origin + url.pathname + (param ? '?' + param : '') + url.hash)
    }
    }

    var parseQuery = function(param) {
    let parseQuery = (param) => {
    return param.split('&')
    .map(function(pair) { return pair.split('=') })
    .reduce(function(o, p) { o[p[0]] = decodeURIComponent(p[1]); return o }, {})
    .map((pair) => pair.split('='))
    .reduce((o, p) => { o[p[0]] = decodeURIComponent(p[1]); return o }, {})
    }

    var toParam = function(object) {
    let toParam = (object) => {
    return Object.keys(object)
    .map(function(key) { return key + '=' + encodeURIComponent(object[key]) })
    .map((key) => key + '=' + encodeURIComponent(object[key]))
    .join('&')
    }

    var removeLinkSpm = function(link) {
    removeSpm(link, function(newUrl) {
    let removeLinkSpm = (link) => {
    removeSpm(link, (newUrl) => {
    link.href = newUrl
    })
    }

    var removeLocationSpm = function() {
    removeSpm(location, function(newUrl) {
    let removeLocationSpm = () => {
    removeSpm(location, (newUrl) => {
    history.replaceState(null, document.title, newUrl)
    })
    }

    var isLink = function(node) {
    let isLink = (node) => {
    return node && node.nodeName === 'A'
    }

    var observer = new MutationObserver(function(mutations) {
    mutations.forEach(function(mutation) {
    var type = mutation.type
    let observer = new MutationObserver((mutations) => {
    mutations.forEach((mutation) => {
    let type = mutation.type
    if (type === 'attributes') {
    if (mutation.attributeName === 'href' && isLink(mutation.target)) {
    removeLinkSpm(mutation.target)
    }
    } else if (type === 'childList' || type === 'subtree') {
    each.call(mutation.addedNodes, function(node) {
    forEach(mutation.addedNodes, (node) => {
    if (isLink(node)) {
    removeLinkSpm(node)
    }
    @@ -65,6 +66,6 @@ var observer = new MutationObserver(function(mutations) {
    })
    })

    observer.observe(document.body, { attributes: true, childList:true, subtree: true })
    each.call(document.links, removeLinkSpm)
    observer.observe(document.body, {attributes: true, childList: true, subtree: true})
    forEach(document.links, removeLinkSpm)
    removeLocationSpm()
  4. @ambar ambar revised this gist Apr 2, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions nospm.user.js
    Original file line number Diff line number Diff line change
    @@ -16,14 +16,14 @@ var removeSpm = function(url, next) {
    if (query.spm) {
    delete query.spm
    var param = toParam(query)
    next('//' + url.host + url.pathname + (param ? '?' + param : ''))
    next(url.origin + url.pathname + (param ? '?' + param : '') + url.hash)
    }
    }

    var parseQuery = function(param) {
    return param.split('&')
    .map(function(pair){ return pair.split('=') })
    .reduce(function(o, p){ o[p[0]] = decodeURIComponent(p[1]); return o }, {})
    .map(function(pair) { return pair.split('=') })
    .reduce(function(o, p) { o[p[0]] = decodeURIComponent(p[1]); return o }, {})
    }

    var toParam = function(object) {
  5. @ambar ambar created this gist Mar 22, 2014.
    70 changes: 70 additions & 0 deletions nospm.user.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    // ==UserScript==
    // @name nospm
    // @description 移除虾米、淘宝和天猫网址中的 spm 参数(包括地址栏和页面中的链接)
    // @include http://*.xiami.com/*
    // @include http://*.taobao.com/*
    // @include http://*.tmall.com/*
    // ==/UserScript==

    var each = [].forEach

    var removeSpm = function(url, next) {
    var search = url.search
    if (!search) return

    var query = parseQuery(search.slice(1))
    if (query.spm) {
    delete query.spm
    var param = toParam(query)
    next('//' + url.host + url.pathname + (param ? '?' + param : ''))
    }
    }

    var parseQuery = function(param) {
    return param.split('&')
    .map(function(pair){ return pair.split('=') })
    .reduce(function(o, p){ o[p[0]] = decodeURIComponent(p[1]); return o }, {})
    }

    var toParam = function(object) {
    return Object.keys(object)
    .map(function(key) { return key + '=' + encodeURIComponent(object[key]) })
    .join('&')
    }

    var removeLinkSpm = function(link) {
    removeSpm(link, function(newUrl) {
    link.href = newUrl
    })
    }

    var removeLocationSpm = function() {
    removeSpm(location, function(newUrl) {
    history.replaceState(null, document.title, newUrl)
    })
    }

    var isLink = function(node) {
    return node && node.nodeName === 'A'
    }

    var observer = new MutationObserver(function(mutations) {
    mutations.forEach(function(mutation) {
    var type = mutation.type
    if (type === 'attributes') {
    if (mutation.attributeName === 'href' && isLink(mutation.target)) {
    removeLinkSpm(mutation.target)
    }
    } else if (type === 'childList' || type === 'subtree') {
    each.call(mutation.addedNodes, function(node) {
    if (isLink(node)) {
    removeLinkSpm(node)
    }
    })
    }
    })
    })

    observer.observe(document.body, { attributes: true, childList:true, subtree: true })
    each.call(document.links, removeLinkSpm)
    removeLocationSpm()