Hi all--
I'm attempting to implement a feature whereby my Unity webGL app can print out the URLs of all the open tabs in the user's browser. I've managed to write a chrome extension that uses a background .js script to get this information and write it to the browser console. However, I can't quite figure out how to pass this information back into Unity, (or if it's even possible) since it is running in a background script in the extension and not in content script on the webpage.
I've tried using the message-passing functionality suggested by Chrome to communicate with scripts on a webpage. Here's the background js script running in the extension:
var tabURLs = [];
chrome.windows.getAll({populate:true},function(windows){
var i=0;
windows.forEach(function(window){
window.tabs.forEach(function(tab){
//collect all of the urls here, I will just log them instead
tabURLs.push(tab.url);
console.log(tab.url);
i++;
});
});
console.log(i);
});
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.greeting == "hello")
sendResponse({farewell: "goodbye"+tabURLs[0]});
}
);
And here's the jslib function and csharp script I tried to write to receive the data (for now I was just trying to get it to display *one* url just to see if I could get it working):
MessageSender.jslib:
mergeInto(LibraryManager.library, {
SendChromeMessage: function(){
chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
console.log(response.farewell);
});
}
});
Finally, the C# script I'm using to call the jslib function:
public class JSFunctions : MonoBehaviour
{
[DllImport("__Internal")]
public static extern void SendChromeMessage();
public void SendURLs()
{
SendChromeMessage();
}
}
But when I tried running this in a webGL build, I got a whole bunch of errors:
![alt text][1]
Any ideas on how to do this? Or is what I'm attempting to do just not possible given how Unity and webGL work? Thanks!
[1]: /storage/temp/177950-unity-webgl-errors.png
↧