簡體   English   中英

如何獲得當前的 Crome URL?

[英]How can I get the current Crome URL?

我正在 C# 上編寫 Windows Forms 應用程序,並試圖從 Chrome 中獲取當前的 ZE6B391A8D2C4D45902A23A 應用程序。 我找到了活動進程,如果它的名稱是“chrome”,我會嘗試任何方法,但都沒有成功。 我發現的所有解決方案僅適用於以前版本的 Chrome。 據我了解,最好使用谷歌瀏覽器擴展(但我從來沒有寫過它們,而且我對 JS 很熟悉)。

所以,我試着寫一個擴展:

清單.js:

{
"manifest_version": 3,
"name": "URL collector",
"version": "0.11",
"description": "Собирает URL посещенных ресурсов",
"permissions": [
    "tabs",
    "activeTab",
    "webNavigation",
    "scripting"
],
"background": {
    "service_worker": "background.js"
  },
"content_scripts": [
    {
        "matches": [
            "http://*/*",
            "https://*/*"
        ],
        "js": [
            "app.js"
        ]
    }
]}

背景.js:

chrome.runtime.onInstalled.addListener(() => {
async function getCurrentTab() {
    let queryOptions = { active: true, lastFocusedWindow: true };
    let [tab] = await chrome.tabs.query(queryOptions);
    return tab;
}
alert(window.getCurrentTab());});

應用程序.js:

chrome.tabs.query({active: true, lastFocusedWindow: true}, function(tabs) {
var tab = tabs[0];
console.log(tab.url);
alert(tab.url);});

我也試過這樣的代碼:

;(function() {
var pushState = history.pushState;
var replaceState = history.replaceState;

history.pushState = function() {
    pushState.apply(history, arguments);
    window.dispatchEvent(new Event('pushstate'));
    window.dispatchEvent(new Event('locationchange'));
};

history.replaceState = function() {
    replaceState.apply(history, arguments);
    window.dispatchEvent(new Event('replacestate'));
    window.dispatchEvent(new Event('locationchange'));
};

window.addEventListener('popstate', function() {
    window.dispatchEvent(new Event('locationchange'))
});})();

window.addEventListener('locationchange', function(){
    console.log('onlocationchange event occurred!');
    alert(chrome.tabs.getCurrent(tab => tab.url));})

我在 app.js 中嘗試過的所有東西我也在 background.js 中嘗試過,反之亦然。 而且要么我不明白如何跟蹤擴展觸發,要么它不起作用(可能是第二個)。 一般來說,我試圖捕捉 URL 更改事件,例如,切換選項卡或點擊鏈接。 好吧,到目前為止,什么都沒有發生。 實際上,第一個問題是:如何進行這樣的擴展?

看來,我完全不了解該主題,因此我也將非常感謝有關該主題的材料的鏈接。

那么,第二個問題是,將 URL 傳遞給 Windows Forms 應用程序的最佳方法是什么?

對不起我的英語,它似乎真的很糟糕。

在我的 app.js 中,我使用current.Window而不是lastFocusedWindow

button_element.addEventListener('click', () => {    
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
        arr.push(tabs[0].url)
        localStorage.setItem("example", JSON.stringify(arr) )
        render(arr)
    })
})

這適用於將當前選項卡推送到本地存儲

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM