1
votes

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 background.html. Below is my code.

background.js

var port = chrome.runtime.connectNative('com.example.native');

function MyCurrentTabs(tab) 
{
     getUrl( tab.title, tab.url );
}   
function onActivate(activeInfo) 
{
     chrome.tabs.get(activeInfo.tabId, MyCurrentTabs);
}   
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);
    }       
}

chrome.tabs.onActivated.addListener(onActivate);
chrome.windows.onFocusChanged.addListener(function(windowId) {
{
    if (windowId != chrome.windows.WINDOW_ID_NONE) 
    {
        chrome.tabs.query({ active:true, windowId:windowId }, function(tabs) 
        {
            if (tabs.length === 1) 
            {
                MyCurrentTabs(tabs[0])
            }
    });
} else
     getUrl("","");
});

manifest.json

{
    "name": "watchURL",
    "description": "The active url read",
    "version": "2.0",
    "permissions": [
    "tabs",
     "nativeMessaging"
    ],
    "background": {
    "scripts": ["background.js"],
    "persistent": false
    },
    "browser_action": {
    "default_title": "The active url read",
    "default_popup": "background.html" 

    },
    "manifest_version": 2
}

background.html

<html><body><script type="text/javascript" src="background.js"></script></body></html>

Then create Host native json file for native messaging. Below is my code.

{
"name": "com.example.native",
"description": "Native support for Chrome Extension",
"path": "ChromeExt.exe",
"type": "stdio",
"allowed_origins": [
"chrome-extension://**********************/"
]
}

After that create registry entry for native messaging with same name (com.example.native).

The console application code is below----------------

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 chrome application exe file inside the system32 folder. But when I am going to open the www.google.com tab and then www.yahoo.com at the chrome browser nothing happened. Even I have written a file and kept it at the system32 folder while opening any tab at google chrome browser but the file has not been written.

I will publish my extension later on google store once it is manually done successfully.

Now my question is how to execute this console application and fetch the Title and URL from the JS file through native messaging. Shall I have to run the console application.

What I am supposed to do now. Please help me in this regard.

Any kind of help will be highly appreciated.

1
Look for error messages on the Chrome side. In particular, the background console reachable through chrome://extensions/ - Xan
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. - Tutun

1 Answers

1
votes

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 and also fetching the active tab as well as url automatically whenever I am working with Google Chrome.