I am trying to get active Tab Title as well as URL from Google Chrome. I have installed Chrome extension manually and created a console application for getting the active tab title and url.
First of all I have installed Chrome Extension which contains file like manifest.json, background.js and popup.html. Below is my code.
background.js
var port = chrome.runtime.connectNative('com.example.native');
function onActivate(activeInfo)
{
chrome.tabs.get(activeInfo.tabId, MyCurrentTab);
}
function getUrl(title, url)
{
var o = { title: title, url: url };
try
{
port.postMessage(o);
}
catch(err)
{
port = chrome.runtime.connectNative('com.example.native');
port.postMessage(o);
}
}
function MyCurrentTab(tab)
{
getUrl( tab.title, tab.url );
}
chrome.tabs.onActivated.addListener(onActivate);
manifest.json
{
"name": "watchURL",
"description": "Reading Active Chrome URL",
"version": "2.0",
"permissions": [
"tabs",
"nativeMessaging"
],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"browser_action": {
"default_title": "The active url read",
"default_popup": "popup.html"
},
"manifest_version": 2
}
Then I have created the host native json file for native messaging. Below is the code.
{
"name": "com.example.native",
"description": "Native support for Chrome Extension",
"path": "ChromeExt.exe",
"type": "stdio",
"allowed_origins": [
"chrome-extension://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/"
]
}
After that, I have created registry entry for native messaging with same name (com.example.native).
The code for C# console application is shown below,
private static string OpenStandardStreamIn()
{
Stream stdin = Console.OpenStandardInput();
int length = 0;
byte[] bytes = new byte[4];
stdin.Read(bytes, 0, 4);
length = System.BitConverter.ToInt32(bytes, 0);
string input = "";
for (int i = 0; i < length; i++)
{
input += (char)stdin.ReadByte();
}
return input;
}
I have kept the native host json file and C# application .exe file under the system32 folder. But when I am going to open www.google.com or then www.yahoo.com at the chrome browser, nothing happens. I have also written code for a file dump inside that C# app but no luck.
I will publish my extension later on google store once it is manually done successfully.
Now my question is how I need to execute this console application and fetch the Title and URL from the Chrome Extension.
What I am supposed to do now? Please help me in this regard.
Any kind of help will be highly appreciated.