achorein / expo-share-intent

🚀 Simple share intent in an Expo Native Module
MIT License
119 stars 10 forks source link

More text when sharing a link? #38

Closed RobinHeidenis closed 3 months ago

RobinHeidenis commented 3 months ago

Hello! This library is exactly what I needed for my new project, thank you so much for this work.

When sharing a link to an application like google keep and some other apps from the browser, it includes the page title for example. In other cases, some extra text gets added to the shared link. However, when sharing links to my app using this library, that extra text does not show up. Only the link actually gets passed.

My app.json is setup like this:

[
   "expo-share-intent",
   {
      "androidIntentFilters": ["text/*"]
   }
]

When logging the incoming shareIntent object, like this:

useEffect(() => {
    alert(JSON.stringify(shareIntent));
  }, [shareIntent]);

It lists no files (as expected), and the same text and webUrl content (just the shared url). The type is text.

Would it be possible to allow that extra text to be shared to this library as well? I have no idea if this would be a trivial change or if I'm doing something wrong. I'd be happy to provide more information if necessary. I'm only building for Android, and have only tested on my Pixel 7 Pro.

Thank you so much!

achorein commented 3 months ago

Thanks for your feedback.

Just to be sure, the extra information of url (meta data title for exemple) is something available in other app for Android or iOS or both ? If the data exists i can retrieve it but need some digging as is different between the two native module.

In my case i scrap more information using the given url (need image preview) :

    // import { Parser } from 'htmlparser2';
    const html = (await axios.get(webUrl)).data;
    const metas = {};
    const parser = new Parser(
      {
        onopentag(name, attributes) {
          if (name === 'meta') {
            metas[attributes.name || attributes.property] = attributes.content;
          } else if (name === 'img' && attributes['data-old-hires']) {
            metas['og:image'] = attributes['data-old-hires'];
          } else if (name === 'img' && attributes.id === 'main-image') {
            metas['og:image'] = attributes.src;
          }
        },
      },
      { recognizeSelfClosing: true, decodeEntities: false },
    );
    parser.write(html);
    parser.end();
    console.log(metas);
RobinHeidenis commented 3 months ago

So for example when I share one of my websites from my browser to google keep (notes), it shows up like this

image

Google keep then comes up with this screen instantly, making me doubt they're fetching extra information through the network:

image

After clicking save and viewing the note, the title of the note is the same as the title of the webpage

image

This leads me to believe there is probably some extra information being shared. Maybe some other field that's currently not used by this library. I have very little information in Android app development, so I have no idea how it all works precisely. When trying the sharing with all my internet turned off, it still put the page title in the note.

Hopefully this clarifies the situation a bit more. Let me know if I can do anything else to help!

achorein commented 3 months ago

🤔 didn't find the information on Android SDK but seems to have a description value on iOS

RobinHeidenis commented 3 months ago

If i change line 78 of ExpoShareIntentModule.kt from

notifyShareIntent(mapOf( "text" to intent.getStringExtra(Intent.EXTRA_TEXT), "type" to "text"))

to

notifyShareIntent(mapOf( "text" to intent.getCharSequenceExtra(Intent.EXTRA_TITLE), "type" to "text"))

and use shareIntent.text in my app, I do get the page title. Perhaps it would be possible to add an extra property to the shareIntent object in js, like shareIntent.extra which contains some extra info like this? Or maybe it's possible to send over all the Intent.EXTRA_* properties and let the user of the lib themselves decide which one they need?

RobinHeidenis commented 3 months ago

looks like changing that line 78 to this:

notifyShareIntent(mapOf( "text" to intent.getStringExtra(Intent.EXTRA_TEXT), "type" to "text", "extra" to intent.getCharSequenceExtra(Intent.EXTRA_TITLE)))

and adding this on line 58 of useShareIntent.tsx in the return object (and in the ShareIntent interface):

extra: shareIntent.extra ?? null,

works perfectly for my use case. I can make a PR with these changes if they're okay with you, or if you think something should be done differently I can give it a try. Let me know!

achorein commented 3 months ago

oh great! 🔥

I think we should have something like this for the share intent type (ExpoShareIntentModule.types.ts), will allow use to have more information in the future :

export type ShareIntent = {
  files: ShareIntentFile[] | null;
  text: string | null;
  webUrl: string | null;
  type: "media" | "file" | "text" | "weburl" | null;
  meta?: {
    title?: string
  }
};

same thing on native side, wondering if we can do something like this (not sure) :

notifyShareIntent(mapOf(
  "text" to intent.getStringExtra(Intent.EXTRA_TEXT), 
  "type" to "text", 
  "meta" to mapOf(
      "title" to intent.getCharSequenceExtra(Intent.EXTRA_TITLE)
  )
))

it seems to have a lot of Intent.EXTRA_* 😅, I would like it to be simple as possible, for url we should have only : url, title, meta description and meta image (og:image), like a classic url share on social media.

i would love a PR ❤️ (FYI i just added CONTRIBUTING.md)

RobinHeidenis commented 3 months ago

meta is perfect :D. Is there anything like this for IOS? Or will it just always be empty on that side? I have no experience with swift, so if it's okay i'll just add the android code in this instance. I'll get started on a PR.

achorein commented 3 months ago

this feature will be added in the next release. only works for Android, i have tried for iOS but it's not straight full

achorein commented 2 months ago

@RobinHeidenis added in v1.4.0, thank you for your contribution!