facebook.js 1.8 KB

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