如何禁用chrome打开“新窗口”和“标签”? [英] How to disable chrome from opening up "new window" and "tabs"?

查看:331
本文介绍了如何禁用chrome打开“新窗口”和“标签”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

他们是否通过Chromes浏览器设置将所有网页保存在一个窗口中?或一个插件/插件,我可以做到这一点?



我不想在新标签和/或新窗口打开网页时,我点击一些链接。



让我知道是否有人有任何建议!,谢谢!!

 < a href =http://www.bing.comtarget =_ blank>打开新标签!< / a> 

< i>并非我想要的东西...,我希望这个链接留在同一网址栏中。< / i>

< p>像这样!< / p>
< a class =clickhref =http://www.bing.com>点击此处< / a>


解决方案

可能的方法:



一种方法可能是构建一个扩展,用于注入 内容脚本 。此内容脚本将解析DOM并从锚元素中移除所有目标属性并设置所有目标锚元素的属性添加到 _self 中。

警告:




  1. 动态插入元素(包括锚元素)
  2. 在某些页面上有动态变化的元素(包括锚点元素)。
  3. 不是所有的页面/标签都通过链接打开(例如某些页面可以使用 窗口。 open() )。


  4. 解决方案:



    您可以使用 MutationObserver ,用于监视插入的锚元素或修改其 target 属性并进行相应调整。



    如果它对您来说非常重要,您仍然需要照顾通过其他方式打开的选项卡(例如 window.open())应该非常少,所以它可能不值得麻烦)。



    示例代码:



    清单。 json:

      {
    manifest_version:2,

    name:Test Extension,
    version:0.0,

    content_scripts:[{
    matches:[*:' // * / *],
    js:[content.js],
    run_at:document_start,
    all_frames:true
    } ]
    }

    content.js:

      / *定义助手函数* / 
    var processAnchor = function(a){
    // if(a.hasAttribute('target')){
    // a.removeAttribute('target');
    //}
    a.setAttribute('target','_self');
    };

    定义用于观察插入元素的观察者* /
    var insertedObserver = new MutationObserver(function(mutations){
    mutations.forEach(function(m){
    var插入= [] .slice.call(m.addedNodes);
    while(inserted.length> 0){
    var elem = inserted.shift();
    [] .slice.call(elem.children || [])。forEach(function(el){
    inserted.push(el);
    });
    if(elem.nodeName == ='A'){
    processAnchor(elem);
    }
    }
    });
    });

    定义用于观察
    *修饰的锚元素属性的观察者* /
    var modifiedObserver = new MutationObserver(函数(突变){
    mutations.forEach (函数(m){
    if((m.type ==='attributes')&&& amp;(m.target.nodeName ==='A')){
    processAnchor(m。目标);
    }
    });
    });
    $ b $ * *开始观察* /
    insertedObserver.observe(document.documentElement,{
    childList:true,
    subtree:true
    });
    modifiedObserver.observe(document.documentElement,{
    attributes:true,
    substree:true
    });


    Is their a way to keep all pages on the internet in a single window through Chromes browser settings? or a addon/plugin that I can do this with?

    I don't want web pages to open up in new tabs and/or new windows when I click on some links.

    Let me know if anyone has any suggestions!, Thanks!!

            <a href="http://www.bing.com" target="_blank">Opens a New Tab!</a>
    
            <i>Thtats not what i want..., I want this link to stay in same url bar.</i>
    
            <p>Like this!</p>
            <a class="click" href="http://www.bing.com">Click Here</a>
    

    解决方案

    Possible approach:

    One approach could be building an extension that injects a content script in every page. This content script would parse the DOM and remove all target attributes off the anchor elementsand set all target attributes of anchor elements to _self.

    Caveats:

    1. There are dynamically inserted elements (including anchor elements) on many pages.
    2. There are dynamically changing elements (including anchor elements) on some pages.
    3. Not all pages/tabs are opened through links (e.g. some page could use window.open()).

    Solution:

    You could use a MutationObserver that watches for anchor elements being inserted or having their target attribute modified and make the appropriate adjustments.

    You still need to take care of tabs opened by other means (e.g. window.open()) if it is extremely important to you (but those cases should be very very few, so it might not be worth the trouble).

    Sample code:

    manifest.json:

    {
        "manifest_version": 2,
    
        "name":    "Test Extension",
        "version": "0.0",
    
        "content_scripts": [{
            "matches":    ["*://*/*"],
            "js":         ["content.js"],
            "run_at":     "document_start",
            "all_frames": true
        }]
    }
    

    content.js:

    /* Define helper functions */
    var processAnchor = function(a) {
        //if (a.hasAttribute('target')) {
        //    a.removeAttribute('target');
        //}
        a.setAttribute('target', '_self');
    };
    
    /* Define the observer for watching over inserted elements */
    var insertedObserver = new MutationObserver(function(mutations) {
        mutations.forEach(function(m) {
            var inserted = [].slice.call(m.addedNodes);
            while (inserted.length > 0) {
                var elem = inserted.shift();
                [].slice.call(elem.children || []).forEach(function(el) {
                    inserted.push(el);
                });
                if (elem.nodeName === 'A') {
                    processAnchor(elem);
                }
            }
        });
    });
    
    /* Define the observer for watching over
     * modified attributes of anchor elements */
    var modifiedObserver = new MutationObserver(function(mutations) {
        mutations.forEach(function(m) {
            if ((m.type === 'attributes') && (m.target.nodeName === 'A')) {
                processAnchor(m.target);
            }
        });
    });
    
    /* Start observing */
    insertedObserver.observe(document.documentElement, {
        childList: true,
        subtree: true
    });
    modifiedObserver.observe(document.documentElement, {
        attributes: true,
        substree: true
    });
    

    这篇关于如何禁用chrome打开“新窗口”和“标签”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆