show.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { reportLog } from '../logger';
  2. import { getActiveKeyAfterClick } from '@/uilts/help';
  3. // 每个窗口共享一个Observer实例
  4. class ShowLogObserver {
  5. constructor() {
  6. this._observe = null;
  7. this.showLogMap = new Map();
  8. this.init();
  9. }
  10. init() {
  11. this._observe = new IntersectionObserver((entries, observer) => {
  12. entries.forEach((entry) => {
  13. if (entry.isIntersecting) {
  14. this.report(entry);
  15. // show-log-once === '1' && 曝光之后取消观察
  16. if (entry?.target?.getAttribute('show-log-once') === '1') {
  17. this.remove(entry.target);
  18. }
  19. }
  20. })
  21. }, {
  22. root: null,
  23. rootMargin: '0px',
  24. threshold: 1
  25. })
  26. }
  27. remove(el) {
  28. this._observe.unobserve(el);
  29. this.showLogMap.delete(el.denetShowLogkey);
  30. }
  31. add(el, binding) {
  32. this._observe.observe(el);
  33. this.showLogMap.set(el.denetShowLogkey, binding.value)
  34. }
  35. report(el) {
  36. const logData = this.showLogMap.get(el?.target?.denetShowLogkey);
  37. return logData && reportLog({
  38. businessType: Report.businessType.pageView,
  39. ...logData
  40. })
  41. }
  42. }
  43. export default new ShowLogObserver();