1

Google Chrome からアクティブなタブ タイトルと URL を取得しようとしています。Chrome 拡張機能を手動でインストールし、アクティブなタブのタイトルと URL を取得するためのコンソール アプリケーションを作成しました。

まず、manifest.json、background.js、background.html などのファイルを含む Chrome 拡張機能をインストールしました。以下は私のコードです。

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("","");
});

マニフェスト.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>

次に、ネイティブ メッセージング用のホスト ネイティブ json ファイルを作成します。以下は私のコードです。

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

その後、同じ名前 (com.example.native) でネイティブ メッセージングのレジストリ エントリを作成します。

コンソール アプリケーションのコードは次のとおりです----------------

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;
}

ネイティブ ホストの json ファイルと chrome アプリケーションの exe ファイルを system32 フォルダー内に保持しています。しかし、www.google.com タブを開いてから、chrome ブラウザで www.yahoo.com を開こうとしても、何も起こりませんでした。Google Chromeブラウザで任意のタブを開きながらファイルを書き込んでsystem32フォルダに保存したのですが、ファイルが書き込まれていません。

手動で正常に完了したら、拡張機能を後で Google ストアに公開します。

ここでの質問は、このコンソール アプリケーションを実行し、ネイティブ メッセージングを介して JS ファイルからタイトルと URL を取得する方法です。コンソール アプリケーションを実行する必要がありますか。

私が今やるべきこと。この点で私を助けてください。

どんな種類の助けも高く評価されます。

4

1 に答える 1