我正在使用Cocos Creator为Web浏览器创建一个简单的游戏,并且需要实现共享到twitter按钮。但是我需要用普通的javascript做到这一点。到目前为止,我有此代码。
cc.Class({
extends: cc.Component,
properties: {
tweet: cc.Button
},
start() {
const el = document.createElement('a');
el.classList.add('twitter-share-button');
el.href = 'https://twitter.com/intent/tweet?url=http://test.com;via=stack';
el.id = 'twiiterBtn';
// After appending to body, element exists.
document.body.appendChild(el);
window.twttr = (function(d, s, id) {
var js,
fjs = d.getElementsByTagName(s)[0],
t = window.twttr || {};
if (d.getElementById(id)) return t;
js = d.createElement(s);
js.id = id;
js.src = 'https://platform.twitter.com/widgets.js';
fjs.parentNode.insertBefore(js, fjs);
t._e = [];
t.ready = function(f) {
t._e.push(f);
};
return t;
})(document, 'script', 'twitter-wjs');
window.twttr.ready(function(twttr) {
twttr.events.bind('tweet', function(event) {
console.log('tweet callback', event);
});
});
this.tweet.node.on('click', this.dispatch);
},
dispatch() {
// Everytime I click the button, this function gets called correctly
// but the element doesn't exist anymore, Cocos creator removed it.
console.log(document.getElementById('twiiterBtn'))
}
});
在start()
,我创建了元素并添加了旋转按钮所需的类,window.twttr可以正常工作,可以执行ready函数,但是Cocos创建者正在删除我的元素,由于该元素不再存在,因此回调函数不起作用。如果有人遇到类似情况并知道我该如何解决。
我基本上需要实现tweet btn并获取回调。
谢谢
似乎您不知道用户是否发过推文。这里有关它的更多信息:https://stackoverflow.com/a/48803977/4066787
但是这里是用户单击并打开弹出窗口时带回调的有效代码
cc.Class({
extends: cc.Component,
properties: {
tweet: cc.Button
},
start() {
// TODO: text IS CUSTOM MESSAGE; via IS TWITTER USERNAME
const url = encodeURI('https://twitter.com/intent/tweet?text=super custom message;via=TwitterAccount');
const el = document.createElement('a');
el.classList.add('twitter-share-button');
el.href = url;
el.target = '_blank';
el.id = 'twitter-intent';
this.tweet.node.on('click', function(){
window.open(url, '_blank', 'location=yes,height=370,width=520,scrollbars=yes,status=yes');
// TODO: send callback to server.
console.log("call API")
});
}
});