Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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,

C#
private static string OpenStandardStreamIn()
{
    //// We need to read first 4 bytes for length information
    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.
Posted
Updated 19-Sep-19 6:10am
v3
Comments
BillWoodruff 3-Nov-14 1:48am    
"What I am supposed to do now." I would definitely spend some time on the Google/Chrome developer forums: it is very likely somebody else has had the same problem.
Tutun2014 4-Nov-14 12:10pm    
I have done this only after following the forum links and Chrome Extension Dev notes. As I am not an expert in this field, I posted in detail so that it can help you understand whether I am missing something or I have covered all the steps correctly but some error may have occurred and I need to debug it.

I found it. All the codes I posted above are correct. The problem was inside another method of my C# code. I commented that, replaced the newer build of the C# code under system32 folder and the file dump is happening automatically whenever I am working with Google Chrome. No need to run the C# .exe file separately. It took time for me as it was my first time with Chrome Extension development and I was looking for how to debug the whole thing.
 
Share this answer
 
Comments
Mohideenmeera 9-Nov-17 7:12am    
Hi,
Can you please share your code as i am also trying to do the same but no luck.
Hope your code could help me...
monemazeem 6-Jan-22 16:35pm    
Hi,
Can you please share your code as i am also trying to do the same but no luck.
Hope your code could help me...
Do we have to register in registery the manifest.json as described in the chrome document.
I got this doubt as you have missed the point and i am trying to build similar application
 
Share this answer
 
Comments
Richard Deeming 19-Sep-19 13:09pm    
If you want to ask a question, then ASK A QUESTION[^].

DO NOT post your question as a "solution" to someone else's question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900