react-native-share-menu icon indicating copy to clipboard operation
react-native-share-menu copied to clipboard

How to include URL webpage title in share

Open sixmoraltales opened this issue 4 years ago • 25 comments

Is there a way to include the URL webpage title in the share intent data that is shared with app?

sixmoraltales avatar Jan 30 '20 16:01 sixmoraltales

Depends on the app that is sharing the URL, if it includes extra data besides the URL or not. You can check if Intent.EXTRA_SUBJECT is set.

caiosba avatar Jan 30 '20 16:01 caiosba

I'm sharing the URL from a mobile browser chrome for example. Would the webpage title be in Intent.Extra_Subject? Or do I need to parse the title and put it in extra_subject by editing the react-native-share-menu plugin?

sixmoraltales avatar Jan 30 '20 16:01 sixmoraltales

You need to modify the module anyway. You should check if the title is in Intent.EXTRA_SUBJECT, around here: https://github.com/meedan/react-native-share-menu/blob/master/android/src/main/java/com/meedan/ShareMenuModule.java#L54, by adding a line like String title = intent.getStringExtra(Intent.EXTRA_SUBJECT);. Then you can pass the title variable over to the successCallback. If it works, it would be great if you can submit a PR :)

caiosba avatar Jan 30 '20 16:01 caiosba

So something like this? if (Intent.ACTION_SEND.equals(action) && type != null) { if ("text/plain".equals(type)) { String input = intent.getStringExtra(Intent.EXTRA_TEXT); String title = intent.getStringExtra(Intent.EXTRA_SUBJECT); successCallback.invoke(input, title);

sixmoraltales avatar Jan 30 '20 16:01 sixmoraltales

Yes! on the callback side, please make sure that title is not mandatory.

caiosba avatar Jan 30 '20 16:01 caiosba

Any recommendations on how I would ensure it's not mandatory?

sixmoraltales avatar Jan 30 '20 17:01 sixmoraltales

I mean, just make sure that the callback still works if the title is not present.

caiosba avatar Jan 30 '20 17:01 caiosba

With this adaptation of the .getSharedText function, the value of "title" is undefined after share action from browser (but perhaps there's something wrong with the way I added "title: string" ?):

ShareMenu.getSharedText((text: string, title: string) => { console.log("shared data is: " + text + " and " + title) if (text && text.length) { var share = { link: text, comment: title }; that.props.navigation.navigate("AndroidShareScreen", { share: share }); } });

sixmoraltales avatar Jan 30 '20 18:01 sixmoraltales

Probably not set by the browser then :(

caiosba avatar Jan 30 '20 19:01 caiosba

Is there a way to get the title of the webpage? In javascript this is quite simple. It must be possible on Android!

sixmoraltales avatar Jan 30 '20 19:01 sixmoraltales

A possible hack is to load the URL you receive in a hidden WebView and get the title from inside it:

handleMessage(message) {
  // message.nativeEvent.data contains the page title
  console.log(message.nativeEvent.data);
}

render() {
  return (
    <WebView
      source={{ uri: "put here the URL from the intent" }}
      injectedJavaScript="window.postMessage(document.title)"
      onMessage={this.handleMessage}
    />
  )
}

caiosba avatar Jan 30 '20 19:01 caiosba

Ah, good idea. I'll try it.

sixmoraltales avatar Jan 30 '20 19:01 sixmoraltales

Oh, but this will create delay in the display of the webpage title. So it will be necessary to pass the URL to a view that invisibly loads the page completely to get the title, and then invisibly loads another view that displays the Title and the URL allowing the user to confirm what they wanted to share. Seems very complicated for a simple share action :(

sixmoraltales avatar Jan 30 '20 19:01 sixmoraltales

Yeah, any approach to get the title from the target app side will have a delay because you'll need to basically load the URL again... that's why in the first place I suggested to try to see if the source app provides the title already... too bad it doesn't :(

caiosba avatar Jan 30 '20 19:01 caiosba

Thank a lot for your help! I can't believe Chrome doesn't provide the webpage title to the share action. Sheesh.

sixmoraltales avatar Jan 30 '20 19:01 sixmoraltales

You're welcome, good luck. If you find another solution please update here.

caiosba avatar Jan 30 '20 19:01 caiosba

Hmm. According to this, the title should be included by Chrome: https://paul.kinlan.me/parsing-screenshot-from-chrome-for-android-send-intent/

sixmoraltales avatar Jan 30 '20 19:01 sixmoraltales

Ok, so it does grab the title and the URL. I just can't get both through the callback with successCallback.invoke(input, title). Any suggestions on how to get two values through the callback?

sixmoraltales avatar Jan 30 '20 21:01 sixmoraltales

Passing an Array to the callback doesn't seem to work. Admittedly I'm not very familiar with Java...

if (Intent.ACTION_SEND.equals(action) && type != null) {

  if ("text/plain".equals(type)) {
    String input = intent.getStringExtra(Intent.EXTRA_TEXT);
    String title = intent.getStringExtra(Intent.EXTRA_SUBJECT);
    String[] values = {input, title};
    successCallback.invoke(values);

sixmoraltales avatar Jan 30 '20 22:01 sixmoraltales

Reopening and labeling as "feature".

caiosba avatar Feb 02 '20 22:02 caiosba

i ve used this package react-native-receive-sharing-intent

ajith-ab avatar May 31 '20 18:05 ajith-ab

Hello @ajith-ab it looks that the package that you claim to be using is actually your own creation. Sounds like a disingenuous way to advertise for your module, by tagging every open issue here! It's also a pity you chose to create your own package from scratch, instead of contributing to an existing one.

infojunkie avatar May 31 '20 18:05 infojunkie

Hello @infojunkie I commented here due to this package cannot be updating by the Author and this package is only available for image and text. I have written package is Supports file, image & text share receiving . So here i will Commented here. I have created a package from scratch @infojunkie please compare this two packages

ajith-ab avatar Jun 01 '20 10:06 ajith-ab

Did anyone find the solution here? I need to extract the URL and Title from the browser after sharing selected text.

Thanks in advance!

qsdamar avatar Feb 24 '21 21:02 qsdamar

just put these lines

//keys final String EXTRA_DATA_KEY = "extra"; //created new vri for extra

if (Intent.ACTION_SEND.equals(action)) { if ("text/plain".equals(type)) { data.putString(DATA_KEY, intent.getStringExtra(Intent.EXTRA_TEXT));

//add this line 
    data.putString(EXTRA_DATA_KEY,intent.getStringExtra(Intent.EXTRA_SUBJECT));

    return data;
  }

I hope this will be helpful for you
, connect with me on Linkedin jayesh karma

jayeshkarma100 avatar Aug 17 '21 06:08 jayeshkarma100