繁体   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