vue-wechat-title.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. (function () {
  2. function install (Vue) {
  3. var setWechatTitle = function (title, img) {
  4. if (title === undefined || window.document.title === title) {
  5. return
  6. }
  7. document.title = title
  8. var mobile = navigator.userAgent.toLowerCase()
  9. if (/iphone|ipad|ipod/.test(mobile)) {
  10. var iframe = document.createElement('iframe')
  11. iframe.style.display = 'none'
  12. // 替换成站标favicon路径或者任意存在的较小的图片即可
  13. iframe.setAttribute('src', img || 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7')
  14. var iframeCallback = function () {
  15. setTimeout(function () {
  16. iframe.removeEventListener('load', iframeCallback)
  17. document.body.removeChild(iframe)
  18. }, 0)
  19. }
  20. iframe.addEventListener('load', iframeCallback)
  21. document.body.appendChild(iframe)
  22. }
  23. }
  24. Vue.directive('wechat-title', function (el, binding) {
  25. setWechatTitle(binding.value, el.getAttribute('img-set') || null)
  26. })
  27. }
  28. if (typeof exports === 'object') {
  29. module.exports = install
  30. } else if (typeof define === 'function' && define.amd) {
  31. define([], function () {
  32. return install
  33. })
  34. } else if (window.Vue) {
  35. Vue.use(install)
  36. }
  37. })()