I'm working on creating a basic chrome extension that features an icon. When the icon is clicked, I want the official Twitter window to pop up (similar to what you see here). One common issue with existing extensions is that the Twitter window remains open after use. By including this script provided by Twitter, they handle closing the window automatically after a few seconds. This is the functionality I am aiming for, so I am attempting to inject that code and execute the URL. Please note that my knowledge of both JavaScript and chrome extensions is limited.
This is my current progress:
function onClicked(tab) {
var twitterWidgets = document.createElement("script");
twitterWidgets.type = "text/javascript";
twitterWidgets.src = "https://platform.twitter.com/widgets.js";
var head = document.getElementsByTagName("head")[0];
head.appendChild(twitterWidgets);
var urlToTweet = "https://twitter.com/intent/tweet?"
+ "text=" + encodeURIComponent(tab.title)
+ "&url=" + encodeURIComponent(tab.url);
//window.open(urlToTweet);
}
chrome.browserAction.onClicked.addListener(onClicked);
window.open opens a new tab which isn't suitable, while window.location does not seem to work at all. I understand that I may need to incorporate chrome.extension.getURL into twitterWidgets.src, or something similar. However, the more I try to modify the script, the more confused I become.
Any guidance on the right approach would be highly appreciated.