Chrome Extension: Open new tab without losing popup focus

前端 未结 2 1501
面向向阳花
面向向阳花 2020-12-21 08:38

I have a simple list of research links on a Chrome Extension:

Research Link 1


        
相关标签:
2条回答
  • 2020-12-21 09:11

    Yes, there is an option for create:

    chrome.tabs.create({url: 'http://www.google.com', active: false});
    

    I'm using it in one of my extensions exactly as you described.

    0 讨论(0)
  • 2020-12-21 09:23

    Here is what I ended up doing to allow a proper ctrl+click (open tab in new background window) in a Chrome Extension - uses jQuery.

    <a class="ctrllink" style="cursor: pointer;" url="http://www.example1.com">Research Link 1</a>
    <a class="ctrllink" style="cursor: pointer;" url="http://www.example2.com">Research Link 2</a>
    <a class="ctrllink" style="cursor: pointer;" url="http://www.example3.com">Research Link 3</a>
    

    I used the <a> tag in order to maintain the default hyperlink style (color, hover underline, etc) but removed the href. Since there is no href, need to define the cursor style in the element or class.

    Global variable:

    var tabplacement = 0;
    

    Tabplacement was done to simulate the way Chrome opens tabs, incrementing from the last created.

    $(function () {
        $('.ctrllink').on('click', function (event) {
            var ctrlpressed = (event.ctrlKey || event.metaKey);
            var url = $(this).attr('url');
            chrome.tabs.getSelected(null, function (tab) {
                tabplacement += 1;
                var index = tab.index + tabplacement;
                chrome.tabs.create({'url': url, active: !ctrlpressed, 'index': index});
            });
        });
    });
    

    Included the metaKey to handle Mac Command ⌘

    0 讨论(0)
提交回复
热议问题