Tieba No App

解除百度贴吧强制 App 跳转

当前为 2024-05-05 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name					Tieba No App
// @namespace			[email protected]
// @author				依然独特
// @description		解除百度贴吧强制 App 跳转
// @version				0.0.1
// @run-at				document-end
// @include				https://tieba.baidu.com/f*
// @include				https://tieba.baidu.com/p/*
// @match					https://tieba.baidu.com/f*
// @match					https://tieba.baidu.com/p/*
// @grant					unsafeWindow
// @license				CC-BY-4.0
// ==/UserScript==

(function () {
  // Only on desktop site `PageData' will be defined
  if (unsafeWindow.PageData != null) {
    return
  } else {
    // Make Tieba thinks it is in baidu box app
    Object.defineProperties(navigator, Object.getOwnPropertyDescriptors({
      userAgent: 'Mozilla/5.0 baiduboxapp0'
    }))

    // We need `this' binding
    const filterIframeSet = function (value) {
      const url = new URL(value)

      // Hook up baiduboxapp protocol
      if (url.protocol === 'baiduboxapp:') {
        const data = JSON.parse(decodeURIComponent(url.searchParams.get('params')))

        // Hook up deeplink target
        if (url.pathname === '//v7/vendor/ad/deeplink') {
          const appUrl = new URL(data.appUrl)

          // Hook up Tieba target
          if (appUrl.protocol === 'com.baidu.tieba:') {
            onTiebaUrl(appUrl)
          }
        }

        // Ignore other targets
      } else {
        setIframeSrc.call(this, value)
      }
    }

    const onTiebaUrl = url => {
      const i = url.pathname.lastIndexOf('/')

      const path = url.pathname.slice(2, i)
      const page = url.pathname.slice(i + 1)

      const target_scheme = decodeURIComponent(url.searchParams.get('target_scheme'))

      const data = {
        ...restoreData1(url.searchParams),
        ...restoreData2(page, url.searchParams),
        target_scheme,
      }

      // Real navigation
      if ('pb' === page && data.tid) {
        location.assign(`https://tieba.baidu.com/p/${data.tid}`)
      }

    }

    const restoreData1 = searchParams => {
      const {
        fr: _fr,  // 'bpush'
        bdid, _bdid,
        qd: _qd,  // 'scheme
        downchannel: _downchannel,
        refer, _refer,
        eqid, _eqid,
        obj_locate,
        obj_source,
        obj_name,
        obj_param2,
        has_token,
        extdata,
        ...custom
      } = Object.fromEntries(searchParams)

      return {
        param: {
          obj_locate: obj_locate,
          obj_obj_source: obj_source,
          obj_originSource: obj_name,
          clear_token: has_token,
          extdata: extdata,
          ...custom
        },
        browser: obj_param2,
      }
    }

    const restoreData2 = (page, searchParams) => {
      const t = Object.fromEntries(searchParams)

      if ('pb' === page) {
        if (t.tid) {
          return { tid: t.tid }
        } else {
          return {
            ori_ugc_nid: t.ori_ugc_nid,
            ori_ugc_tid: t.ori_ugc_tid,
            ori_ugc_type: t.ori_ugc_type,
            ori_ugc_vid: t.ori_ugc_vid,
          }
        }
      } else if ('frs' === page && t.kw) {
        return { kw: t.kw }
      } else if ('tbwebview' === page && t.url) {
        return { url: t.url }
      } else if ('usercenter' === page && t.portrait) {
        return { portrait: t.portrait }
      } else if ('topicdetail' === page && t.topic_id) {
        return { topic_id: t.topic_id }
      } else if ('item' === page && t.item_id) {
        return { item_id: t.item_id }
      } else if ('voiceRoom' === page && t.room_id) {
        return { room_id: t.room_id }
      } else if ('router/portal' === page && t.params) {
        return { routerParams: t.params }
      } else if ('searchResultPage' === page && t.query) {
        return { query: t.query }
      }
    }

    // Tieba uses <iframe> src to launch native activity
    const { set: setIframeSrc, ...setIframeAttrs } = Object.getOwnPropertyDescriptor(HTMLIFrameElement.prototype, 'src')

    Object.defineProperty(HTMLIFrameElement.prototype, 'src', {
      ...setIframeAttrs,
      set: filterIframeSet
    })
  }
})()