How can I integrate things like the twitter 'tweet button'?

105 views
Skip to first unread message

Mohammad Al-Quraian

unread,
May 14, 2013, 7:37:21 AM5/14/13
to google-we...@googlegroups.com
Basically, I want to use something like this:

I found this page which suggests loading the script at the beginning, then using jquery to look for certain elements and then renders relevant elements:

I also found this code:

.........
final String scriptStr = "!function(d,s,id){" + " var js,fjs=d.getElementsByTagName(s)[0];"
                                 + " if(!d.getElementById(id)){" + "js=d.createElement(s);"
                                 + " js.id=id;js.src='//platform.twitter.com/widgets.js';"
                                 + " fjs.parentNode.insertBefore(js,fjs);"
                                 + " }}(document,'script','twitter-wjs');";

    final ScriptElement scriptElement = Document.get().createScriptElement(scriptStr);

    // we place the script just after the body tag
    bodyElement.appendChild(scriptElement);

Any suggestions? I want something simple and clean. Thanks.

Thomas Broyer

unread,
May 14, 2013, 7:49:41 AM5/14/13
to google-we...@googlegroups.com
FYI, the code above can be simplified a bit using ScriptInjector:

```
ScriptInjector.fromString(/* the script given by Twitter */)
    .setWindow(ScriptInjector.TOP_WINDOW)
    .inject()
```

And make sure you insert the Anchor in the DOM before injecting the script.

Mohammad Al-Quraian

unread,
May 14, 2013, 11:11:26 AM5/14/13
to google-we...@googlegroups.com
Wow that was easier than I expected. One question though, should I inject the script at the beginning of the app or when I instantiate the presenter? I'm expecting the code of the sharing provider to be accessed quite often.

Thomas Broyer

unread,
May 14, 2013, 11:51:29 AM5/14/13
to google-we...@googlegroups.com

On Tuesday, May 14, 2013 5:11:26 PM UTC+2, Mohammad Al-Quraian wrote:

Wow that was easier than I expected. One question though, should I inject the script at the beginning of the app or when I instantiate the presenter? I'm expecting the code of the sharing provider to be accessed quite often.

The script should be injected each time you add a new Anchor (attached to the doc!) so I'd say you should add it to your view's onAttach.
If it works as well as the one for embedded tweets (which I've used on my blog a few times), and there's no reason it wouldn't, injecting the script multiple times shouldn't cause conflicts: it'll detect Anchors that have already been processed and will only process the others.

Mohammad Al Quraian

unread,
May 15, 2013, 5:18:48 AM5/15/13
to google-we...@googlegroups.com
 Hi Thomas,

The ScriptInjector only works 1% of the times, however this:
Document doc = Document.get();
   
ScriptElement script = doc.createScriptElement();
    script
.setSrc("http://platform.twitter.com/widgets.js");
    script
.setType("text/javascript");
    script
.setLang("javascript");
    doc
.getBody().appendChild(script);
works 100%, do you have any idea why?


--
You received this message because you are subscribed to a topic in the Google Groups "Google Web Toolkit" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-web-toolkit/TjN9oGZlLag/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to google-web-tool...@googlegroups.com.
To post to this group, send email to google-we...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Thomas Broyer

unread,
May 15, 2013, 8:26:40 AM5/15/13
to google-we...@googlegroups.com


On Wednesday, May 15, 2013 11:18:48 AM UTC+2, Mohammad Al-Quraian wrote:
 Hi Thomas,

The ScriptInjector only works 1% of the times, however this:
Document doc = Document.get();
   
ScriptElement script = doc.createScriptElement();
    script
.setSrc("http://platform.twitter.com/widgets.js");
    script
.setType("text/javascript");
    script
.setLang("javascript");
    doc
.getBody().appendChild(script);
works 100%, do you have any idea why?

You mean

```java
ScriptInjector.fromUrl("http://platform.twitter.com/widgets.js")
    .setWindow(ScriptInjector.TOP_WINDOW)
    .inject();
```

? or the one I gave earlier in this thread?
 

Mohammad Al Quraian

unread,
May 16, 2013, 4:38:51 AM5/16/13
to google-we...@googlegroups.com
Well everything seems fine if I injected the script after the anchor is created. The problem now is that the tweets counts are always equal for different urls, and I believe that maybe due to the twitter doesn't consider example.com/#1 different than example.com/#2.

Do you know how can I fix that?


 

--

Thomas Broyer

unread,
May 16, 2013, 4:53:32 AM5/16/13
to google-we...@googlegroups.com


On Thursday, May 16, 2013 10:38:51 AM UTC+2, Mohammad Al-Quraian wrote:
Well everything seems fine if I injected the script after the anchor is created. The problem now is that the tweets counts are always equal for different urls, and I believe that maybe due to the twitter doesn't consider example.com/#1 different than example.com/#2.

Do you know how can I fix that?

Ask Twitter :-)

(or use pustState to have example.com/1 and example.com/2 as URLs, but you'll have to think about IE9- users then…)
 


On Wed, May 15, 2013 at 3:26 PM, Thomas Broyer <t.br...@gmail.com> wrote:


On Wednesday, May 15, 2013 11:18:48 AM UTC+2, Mohammad Al-Quraian wrote:
 Hi Thomas,

The ScriptInjector only works 1% of the times, however this:
Document doc = Document.get();
   
ScriptElement script = doc.createScriptElement();
    script
.setSrc("http://platform.twitter.com/widgets.js");
    script
.setType("text/javascript");
    script
.setLang("javascript");
    doc
.getBody().appendChild(script);
works 100%, do you have any idea why?

You mean

```java
ScriptInjector.fromUrl("http://platform.twitter.com/widgets.js")
    .setWindow(ScriptInjector.TOP_WINDOW)
    .inject();
```

? or the one I gave earlier in this thread?
 

--
You received this message because you are subscribed to a topic in the Google Groups "Google Web Toolkit" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-web-toolkit/TjN9oGZlLag/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to google-web-toolkit+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages