Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get loaded web page title? #192

Closed
yeahia2508 opened this issue Sep 22, 2018 · 8 comments
Closed

How to get loaded web page title? #192

yeahia2508 opened this issue Sep 22, 2018 · 8 comments
Labels
Milestone

Comments

@yeahia2508
Copy link

I have a requirement to change the app title based on the page user currently loaded. is there any way to get page title?

@slightfoot slightfoot added this to the v0.3.1 milestone Nov 15, 2018
@jonnytest1
Copy link

did u try evaluate javascript with document.title ?

@fccoelho
Copy link

did u try evaluate javascript with document.title ?

Sure, but how to get a reference to the Document?

@baoxiehao
Copy link

baoxiehao commented Feb 12, 2019

did u try evaluate javascript with document.title ?

Sure, but how to get a reference to the Document?

You can refer to my code snippet, it works for me:

class WebViewPage extends StatefulWidget {
  final String title;
  final String url;

  const WebViewPage({
    Key key,
    @required this.title,
    @required this.url,
  }) : super(key: key);

  @override
  _WebViewPageState createState() => new _WebViewPageState();
}

class _WebViewPageState extends State<WebViewPage> {
  String title;
  final flutterWebViewPlugin = FlutterWebviewPlugin();

  @override
  void initState() {
    super.initState();
    title = widget.title;
    flutterWebViewPlugin.onStateChanged.listen((state) async {
      if (state.type == WebViewState.finishLoad) {
        String script = 'window.document.title';
        var title = await flutterWebViewPlugin.evalJavascript(script);
        setState(() {
          this.title = title;
          print('loaded TITLE: $title');
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: CustomTheme.buildTheme(),
      home: WebviewScaffold(
        appBar: AppBar(
          title: Text(title),
          leading: IconButton(
            icon: Icon(Icons.arrow_back),
            onPressed: () => Router.pageBack(context),
          ),
        ),
        url: widget.url,
        hidden: true,
        withJavascript: true,
      ),
    );
  }
}

@AlexV525
Copy link

AlexV525 commented Apr 5, 2019

@baoxiehao Hi friend, this works on android, but is this working on iOS? I haven't test yet, but I saw many issues with no javascript in iOS.

@chenxianqi
Copy link

That's how I handled it.

  // get website title
  Future<String> getWebTitle({String url}) async{
    final res = await http.get(url);
    String title = RegExp(r"<[t|T]{1}[i|I]{1}[t|T]{1}[l|L]{1}[e|E]{1}(\s.*)?>([^<]*)</[t|T]{1}[i|I]{1}[t|T]{1}[l|L]{1}[e|E]{1}>").stringMatch(res.data);
    if(title!=null){
      return title.substring(title.indexOf('>')+1, title.lastIndexOf("<"));
    }else{
      return "";
    }
  }


@override
  void initState() {
    super.initState();
    getWebTitle(url: widget.url);
    flutterWebviewPlugin.onUrlChanged.listen((String url) {
      getWebTitle(url: url);
    });
  }

  // use get title
  Future<void> getWebTitle({String url}) async{
    String title = await PublicService().getWebTitle(url: url);
    setState(() {
      this.title = title;
    });
  }

@charafau
Copy link
Collaborator

Seems like it's resolved ?

@fccoelho
Copy link

Haven't tested. But I think it's ok to close this issue.

@charafau
Copy link
Collaborator

Closing..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants