facebook.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { faceShareRedirectUrl } from '@/http/configAPI'
  2. let tempData = {
  3. publishContentStr: ''
  4. }
  5. /**
  6. * 初始化contentjs调用逻辑
  7. */
  8. export function initFacebookContent() {
  9. // window.location.href.indexOf('facebook.com') > -1;
  10. injectShareCode();
  11. shareCallback();
  12. };
  13. /**
  14. * 设置发布内容字符串
  15. */
  16. export function setPublishString(params) {
  17. let { publishContentStr} = params.data || {};
  18. tempData.publishContentStr = publishContentStr;
  19. }
  20. /**
  21. * 注入分享页面逻辑
  22. * @returns
  23. */
  24. function injectShareCode() {
  25. const {href, pathname} = window.location;
  26. if(!(href.indexOf('facebook.com') > -1 && pathname.indexOf('dialog/share'))) {
  27. return;
  28. }
  29. // 设置分享内容
  30. setShareInputContent();
  31. }
  32. /**
  33. * 格式化发布内容字符串
  34. */
  35. function formatContentStr(content) {
  36. let str = content.replace(/(\r)/g,'\n');
  37. return str;
  38. }
  39. /**
  40. * 设置分享输入框内容
  41. */
  42. function setShareInputContent() {
  43. const contentStr = formatContentStr(tempData.publishContentStr);
  44. if (!tempData.isSetContent && contentStr) {
  45. tempData.isSetContent = true;
  46. setTimeout(() => {
  47. document.execCommand("insertText", false, contentStr);
  48. setTimeout(() => {
  49. tempData.isSetContent = false;
  50. }, 800)
  51. }, 600);
  52. }
  53. }
  54. /**
  55. * 分享回调
  56. */
  57. function shareCallback() {
  58. if (window.location.href.indexOf(faceShareRedirectUrl) > -1) {
  59. tempData.publishContentStr = '';
  60. const urlParams = new URLSearchParams(window.location.search);
  61. let params = JSON.parse(urlParams.get('params'));
  62. if (params) {
  63. chrome.runtime.sendMessage({ actionType: "CONTENT_FACEBOOK_SHARE_SUCCESS", data: params }, () => {
  64. })
  65. }
  66. }
  67. }