How to use the just-once function in just-once

To help you get started, we’ve selected a few just-once examples, based on popular ways it is used in public projects.

github fanfoujs / space-fanfou / src / libs / pageDetect.js View on Github external
export function isPrivateMessagePage() {
  return window.location.pathname.split('/')[1] === 'privatemsg'
}

// 是否为随便看看页面
export function isPublicTimelinePage() {
  return window.location.pathname === '/browse'
}

// 是否为管理关注请求页面(加锁时)
export function isFriendRequestPage() {
  return window.location.pathname === '/friend.request'
}

// 判断是否为 timeline 页面,例如首页、用户个人页面、随便看看页面等
export const isTimelinePage = simpleMemoize(() => every([
  // timeline 的页面都有 #stream 元素
  elementReady('#stream'),
  // 管理关注请求页面也有 #stream 元素,而且 DOM 结构和一般 timeline 页面难以区分,排除之
  neg(isFriendRequestPage()),
]))

export function isSharePage() {
  return [ '/sharer', '/sharer/image' ].includes(window.location.pathname)
}
github fanfoujs / space-fanfou / src / libs / pageDetect.js View on Github external
export function isFollowersListPage() {
  return window.location.pathname.split('/')[1] === 'followers'
}

// 是否为用户页面
export const isUserPage = simpleMemoize(() => any([
  isUserProfilePage(),
  isPhotoAlbumPage(),
  isPhotoEntryPage(),
  isFavoritesPage(),
  isFriendsListPage(),
  isFollowersListPage(),
]))

// 是否为当前登录用户页面
export const isLoggedInUserPage = simpleMemoize(() => {
  const userId = getLoggedInUserId()

  return any([
    isLoggedInUserProfilePage(),
    every([
      isUserPage(),
      // TODO: 照片大图页面判断有问题
      window.location.pathname.split('/')[2] === userId,
    ]),
  ])
})

// 是否为私信页面
export function isPrivateMessagePage() {
  return window.location.pathname.split('/')[1] === 'privatemsg'
}
github fanfoujs / space-fanfou / src / libs / pageDetect.js View on Github external
export function isFavoritesPage() {
  return window.location.pathname.startsWith('/favorites/')
}

// 是否为用户好友页面
export function isFriendsListPage() {
  return window.location.pathname.split('/')[1] === 'friends'
}

// 是否为用户粉丝页面
export function isFollowersListPage() {
  return window.location.pathname.split('/')[1] === 'followers'
}

// 是否为用户页面
export const isUserPage = simpleMemoize(() => any([
  isUserProfilePage(),
  isPhotoAlbumPage(),
  isPhotoEntryPage(),
  isFavoritesPage(),
  isFriendsListPage(),
  isFollowersListPage(),
]))

// 是否为当前登录用户页面
export const isLoggedInUserPage = simpleMemoize(() => {
  const userId = getLoggedInUserId()

  return any([
    isLoggedInUserProfilePage(),
    every([
      isUserPage(),
github fanfoujs / space-fanfou / src / libs / pageDetect.js View on Github external
window.location.pathname === '/login'
  )
}

// 是否为当前登录用户的个人页面(消息页面,不含收藏、相册等)
export function isLoggedInUserProfilePage() {
  const userId = getLoggedInUserId()

  return (
    userId &&
    window.location.pathname.split('/')[1] === userId
  )
}

// 是否为当前登录用户用户好友页面
export const isLoggedInUserFriendsListPage = simpleMemoize(async () => {
  if (!isFriendsListPage()) return false

  await elementReady('#stream')

  const urlA = select('.tabs .crumb').href
  const urlB = getLoggedInUserProfilePageUrl()

  return urlA === urlB
})

// 是否为当前登录用户用户粉丝页面
export const isLoggedInUserFollowersListPage = simpleMemoize(async () => {
  if (!isFollowersListPage()) return false

  await elementReady('#stream')
github fanfoujs / space-fanfou / src / libs / pageDetect.js View on Github external
})

// 是否为当前登录用户用户粉丝页面
export const isLoggedInUserFollowersListPage = simpleMemoize(async () => {
  if (!isFollowersListPage()) return false

  await elementReady('#stream')

  const urlA = select('.tabs .crumb').href
  const urlB = getLoggedInUserProfilePageUrl()

  return urlA === urlB
})

// 是否为用户 timeline 页面
export const isUserProfilePage = simpleMemoize(() => {
  return any([
    // 只有用户个人页面中才存在「投诉」对话框
    elementReady('#overlay-report'),
    // 但是当前登录用户的个人页面中没有「投诉」对话框,需要额外判断
    isLoggedInUserProfilePage(),
  ])
})

// 是否为用户相册页面
export function isPhotoAlbumPage() {
  return window.location.pathname.startsWith('/album/')
}

// 是否为照片大图页面
export function isPhotoEntryPage() {
  return window.location.pathname.startsWith('/photo/')
github fanfoujs / space-fanfou / src / background / environment / settings.js View on Github external
const SETTINGS_STORAGE_KEY = 'settings'
const SETTINGS_VERSION_STORAGE_KEY = 'settings/version'
const PREVIOUS_EXTENSION_VERSION_STORAGE_KEY = 'extension-version/previous'
const PREVIOUS_EXTENSION_VERSION_STORAGE_AREA_NAME = 'local'

let optionValuesCache

const getDefaults = simpleMemoize(() => {
  return Object.values(features).reduce((defaultValues, feature) => (
    Object.assign(defaultValues, feature.metadata.defaultValues)
  ), {})
})

const getAllOptionNames = simpleMemoize(() => Object.keys(getDefaults()))

const getOptionDefs = simpleMemoize(() => {
  const unsolderedFeatures = omitBy(features, feature => feature.metadata.isSoldered)
  const optionDefs = mapValues(unsolderedFeatures, feature => feature.metadata.optionDefs)

  return optionDefs
})

const getOptionStorageAreaName = do {
  const fullOptionStorageAreaMap = Object.values(features).reduce((map, feature) => (
    Object.assign(map, feature.metadata.optionStorageAreaMap)
  ), {})

  // eslint-disable-next-line no-unused-expressions
  optionName => fullOptionStorageAreaMap[optionName]
}

function mergeSettings(map) {
github fanfoujs / space-fanfou / src / background / environment / settings.js View on Github external
} from '@constants'

const SETTINGS_STORAGE_KEY = 'settings'
const SETTINGS_VERSION_STORAGE_KEY = 'settings/version'
const PREVIOUS_EXTENSION_VERSION_STORAGE_KEY = 'extension-version/previous'
const PREVIOUS_EXTENSION_VERSION_STORAGE_AREA_NAME = 'local'

let optionValuesCache

const getDefaults = simpleMemoize(() => {
  return Object.values(features).reduce((defaultValues, feature) => (
    Object.assign(defaultValues, feature.metadata.defaultValues)
  ), {})
})

const getAllOptionNames = simpleMemoize(() => Object.keys(getDefaults()))

const getOptionDefs = simpleMemoize(() => {
  const unsolderedFeatures = omitBy(features, feature => feature.metadata.isSoldered)
  const optionDefs = mapValues(unsolderedFeatures, feature => feature.metadata.optionDefs)

  return optionDefs
})

const getOptionStorageAreaName = do {
  const fullOptionStorageAreaMap = Object.values(features).reduce((map, feature) => (
    Object.assign(map, feature.metadata.optionStorageAreaMap)
  ), {})

  // eslint-disable-next-line no-unused-expressions
  optionName => fullOptionStorageAreaMap[optionName]
}
github fanfoujs / space-fanfou / src / content / feature / createSubfeatureClass.js View on Github external
const featureScriptObj = script(this.context) || {}

      if (featureScriptObj.waitReady) {
        this.waitReadyFns.push(featureScriptObj.waitReady)
      }

      this.migrations = featureScriptObj.migrations
      this.script = pick(featureScriptObj, [
        'applyWhen',
        'onLoad',
        'onSettingsChange',
        'onUnload',
      ])
    }

    this.isApplicable = simpleMemoize(
      this.script?.applyWhen ||
      (() => Promise.resolve(true)),
    )
  }
github fanfoujs / space-fanfou / src / background / environment / settings.js View on Github external
SETTINGS_WRITE_ALL,
  SETTINGS_CHANGED,
  STORAGE_CHANGED,
  GET_OPTION_DEFS,
  STORAGE_KEY_IS_EXTENSION_UPGRADED,
  STORAGE_AREA_NAME_IS_EXTENSION_UPGRADED,
} from '@constants'

const SETTINGS_STORAGE_KEY = 'settings'
const SETTINGS_VERSION_STORAGE_KEY = 'settings/version'
const PREVIOUS_EXTENSION_VERSION_STORAGE_KEY = 'extension-version/previous'
const PREVIOUS_EXTENSION_VERSION_STORAGE_AREA_NAME = 'local'

let optionValuesCache

const getDefaults = simpleMemoize(() => {
  return Object.values(features).reduce((defaultValues, feature) => (
    Object.assign(defaultValues, feature.metadata.defaultValues)
  ), {})
})

const getAllOptionNames = simpleMemoize(() => Object.keys(getDefaults()))

const getOptionDefs = simpleMemoize(() => {
  const unsolderedFeatures = omitBy(features, feature => feature.metadata.isSoldered)
  const optionDefs = mapValues(unsolderedFeatures, feature => feature.metadata.optionDefs)

  return optionDefs
})

const getOptionStorageAreaName = do {
  const fullOptionStorageAreaMap = Object.values(features).reduce((map, feature) => (
github fanfoujs / space-fanfou / src / features / status-form-enhancements / fix-dnd-upload@page.js View on Github external
function createTip() {
    tip = (
      <div id="sf-dnd-upload-tip">
        <img src={replaceExtensionOrigin(DROP_ICON_URL)} />
        拖放图片到这里
      </div>
    )
    elementCollection.get('textareaWrapper').append(tip)
  }

  function removeTip() {
    tip.remove()
    tip = null
  }

  const expandTextarea = once(() => {
    const { act, textarea } = elementCollection.getAll()

    act.style.display = 'block'
    textarea.style.height = '4.6em'
  })

  function isDraggingFiles(event) {
    return Array.from(event.dataTransfer.items)
      .some(item => item.kind === 'file')
  }

  function isDraggingImages(event) {
    return Array.from(event.dataTransfer.items)
      .some(item => item.type.startsWith('image/'))
  }

just-once

create a function that can only be invoked once

MIT
Latest version published 2 months ago

Package Health Score

86 / 100
Full package analysis

Popular just-once functions