Chrome 扩展程序获取选项卡 url 时出现问题

发布于 2024-10-20 11:41:16 字数 1514 浏览 1 评论 0原文

我不擅长 JS,而且我遇到了一些 - 我希望 - 愚蠢的问题,我在我的代码中没有看到......如果你们能帮助我,我真的很感激。

我的扩展程序使用当前选项卡的 URL 执行一些操作。在我的后台页面上使用 onUpdate 事件,在变量上设置选项卡的 URL,然后在弹出窗口中使用它,效果很好。

问题是,如果用户开始选择不同的选项卡,而不更新 URL,我的事件将不会再次触发...所以我现在也在监听 onSelectionChanged 事件。

问题是 onSelectionChanged 事件的参数中没有“tab”对象,因此我无法请求 tab.url 属性。

我尝试使用 chrome.tabs.getCurrent() 方法,但显然我做错了......并且我达到了我的知识的极限。

这是代码,如果你们能看一下并为我指出正确的方向,我将非常感激。

<script>
var tabURL = '';

var defaultURLRecognition = [ "test" ];

  // Called when the url of a tab changes.
  function checkForValidUrl(tabId, changeInfo, tab) {

            //THIS IS WHAT'S NOT WORKING, I SUPPOSE
    if (tab==undefined) {
        chrome.tabs.getCurrent(function(tabAux) {
            test = tabAux;
        });
    }
            //

    // If there's no URLRecognition value, I set the default one
    if (localStorage["URLRecognition"]==undefined) {
            localStorage["URLRecognition"] = defaultURLRecognition;
        };
    // Look for URLRecognition value within the tab's URL
    if (tab.url.indexOf(localStorage["URLRecognition"]) > -1) {

    // ... show the page action.
      chrome.pageAction.show(tabId);
      tabURL = tab.url;

    }
  };

  // Listen for any changes to the URL of any tab.
  chrome.tabs.onUpdated.addListener(checkForValidUrl);
  // Listen for tab selection changes
  chrome.tabs.onSelectionChanged.addListener(checkForValidUrl);

</script>

I'm not good at JS and I'm having some -I hope- stupid problem I'm not seeing on my code... if you guys could help me out, I'd really appreciate it.

My extension does some stuff with the current tab's URL. It worked ok using the onUpdate event on my background page, setting the tab's URL on a variable and then I used it on a pop-up.

The thing is that if the user starts, selecting different tabs, without updating the URLs my event won't be triggered again... so I'm now also listening to the onSelectionChanged event.

The thing is that there's no "tab" object within the onSelectionChanged event's parameters, so I cannot ask for the tab.url property.

I tried to use the chrome.tabs.getCurrent() method, but obviously I'm doing something wrong... and I reached the limit of my -very little- knowledge.

Here's the code, if you guys could take a look and point me in the right direction, I'll really appreciate it.

<script>
var tabURL = '';

var defaultURLRecognition = [ "test" ];

  // Called when the url of a tab changes.
  function checkForValidUrl(tabId, changeInfo, tab) {

            //THIS IS WHAT'S NOT WORKING, I SUPPOSE
    if (tab==undefined) {
        chrome.tabs.getCurrent(function(tabAux) {
            test = tabAux;
        });
    }
            //

    // If there's no URLRecognition value, I set the default one
    if (localStorage["URLRecognition"]==undefined) {
            localStorage["URLRecognition"] = defaultURLRecognition;
        };
    // Look for URLRecognition value within the tab's URL
    if (tab.url.indexOf(localStorage["URLRecognition"]) > -1) {

    // ... show the page action.
      chrome.pageAction.show(tabId);
      tabURL = tab.url;

    }
  };

  // Listen for any changes to the URL of any tab.
  chrome.tabs.onUpdated.addListener(checkForValidUrl);
  // Listen for tab selection changes
  chrome.tabs.onSelectionChanged.addListener(checkForValidUrl);

</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃扇骨 2024-10-27 11:41:16

我会做这样的事情:

function checkForValidUrl(tab) {
    //...
}

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){
    if(changeInfo.status == "loading") {
        checkForValidUrl(tab);
    }
});

chrome.tabs.onSelectionChanged.addListener(function(tabId, selectInfo){
    chrome.tabs.getSelected(null, function(tab){
        checkForValidUrl(tab);
    });
});

I would do something like this:

function checkForValidUrl(tab) {
    //...
}

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){
    if(changeInfo.status == "loading") {
        checkForValidUrl(tab);
    }
});

chrome.tabs.onSelectionChanged.addListener(function(tabId, selectInfo){
    chrome.tabs.getSelected(null, function(tab){
        checkForValidUrl(tab);
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文