为什么我的非常简单的Chrome扩展程序可以在Mac上运行而不是PC?

时间:2021-10-12 02:44:01

I have written a very simple Chrome extension. It consists of this background page:

我写了一个非常简单的Chrome扩展程序。它由以下背景页面组成:

<script type="text/javascript">

chrome.tabs.onDetached.addListener(function(tabId, info){
    var id = tabId;
    chrome.tabs.get(id, function(tab) {
        chrome.tabs.create({
            windowId : info.oldWindowId, 
            index : info.oldPosition, 
            url : tab.url
        });
    });
});

</script>

All it does is allows you to pull a tab from a window without losing that tab and web address from the window. It basically duplicates the tab when you detach it.

它所做的就是允许您从窗口中拉出一个选项卡,而不会从窗口中丢失该选项卡和Web地址。它在拆分时基本上复制了选项卡。

The problem is that this works perfectly on a Mac but when I have tried it on two different Windows machines I get this error

问题是这在Mac上完美运行但是当我在两台不同的Windows机器上尝试它时,我得到了这个错误

background.html:7Uncaught TypeError: Cannot read property 'url' of undefined

It appears the tab object isn't being passed into the get callback. Does anyone know why this might be? It obviously is when I run the code on a Mac.

看来tab对象没有传递给get回调。有谁知道为什么会这样?很明显,当我在Mac上运行代码时。

2 个解决方案

#1


1  

The problem is tab id changes after it is detached (old one doesn't exist anymore). Not sure whether it is an error or feature, but if it is inconsistent between Mac and PC then it is definitely an error (could be just performance difference - api method executes faster than tab detaches on a different computer).

分离后的问题是标签ID更改(旧标签不再存在)。不确定它是错误还是功能,但如果Mac和PC之间不一致则肯定是错误(可能只是性能差异 - api方法执行速度比不同计算机上的tab分离更快)。

mrtsherman was on right track with workaround, only instead of saving id you should save info as that id doesn't mean anything anymore. Then you would have all information to recreate a tab (use attached info to get tab id, and saved detached info to get old position and window).

mrtsherman处于正确的解决方法,只是保存id而不是保存id,因为id不再意味着什么。然后,您将获得重新创建选项卡的所有信息(使用附加信息获取选项卡ID,并保存已分离的信息以获取旧位置和窗口)。

#2


1  

So this is the only workaround that I can think of:

所以这是我能想到的唯一解决方法:

  1. OnDetached - store id of tab and also its window id
  2. OnDetached - 存储选项卡的ID以及它的窗口ID
  3. OnAttached - check whether tab id matches stored tab id AND that window id is now different. If so then create new tab in the old window.
  4. OnAttached - 检查选项卡ID是否与存储的选项卡ID匹配,并且窗口ID现在不同。如果是,则在旧窗口中创建新选项卡。

Behavior does seem wonky. Perhaps file a bug report?

行为似乎很不稳定。也许提交错误报告?

#1


1  

The problem is tab id changes after it is detached (old one doesn't exist anymore). Not sure whether it is an error or feature, but if it is inconsistent between Mac and PC then it is definitely an error (could be just performance difference - api method executes faster than tab detaches on a different computer).

分离后的问题是标签ID更改(旧标签不再存在)。不确定它是错误还是功能,但如果Mac和PC之间不一致则肯定是错误(可能只是性能差异 - api方法执行速度比不同计算机上的tab分离更快)。

mrtsherman was on right track with workaround, only instead of saving id you should save info as that id doesn't mean anything anymore. Then you would have all information to recreate a tab (use attached info to get tab id, and saved detached info to get old position and window).

mrtsherman处于正确的解决方法,只是保存id而不是保存id,因为id不再意味着什么。然后,您将获得重新创建选项卡的所有信息(使用附加信息获取选项卡ID,并保存已分离的信息以获取旧位置和窗口)。

#2


1  

So this is the only workaround that I can think of:

所以这是我能想到的唯一解决方法:

  1. OnDetached - store id of tab and also its window id
  2. OnDetached - 存储选项卡的ID以及它的窗口ID
  3. OnAttached - check whether tab id matches stored tab id AND that window id is now different. If so then create new tab in the old window.
  4. OnAttached - 检查选项卡ID是否与存储的选项卡ID匹配,并且窗口ID现在不同。如果是,则在旧窗口中创建新选项卡。

Behavior does seem wonky. Perhaps file a bug report?

行为似乎很不稳定。也许提交错误报告?