javascript - 强制链接在同一窗口/选项卡中打开

标签 javascript jquery html google-chrome kiosk

我正在使用带有此扩展程序的谷歌浏览器 - Open Link in Same Tab .

它非常简单,基本上按照它说的做,强制所有链接在同一个窗口/选项卡中打开。我需要此功能来实现触摸屏信息亭左/右滑动导航。

当涉及到链接时,该插件工作得很好。但是,当在“提交”网络搜索表单中执行搜索时,它不起作用。大概是因为扩展强制所有带有 _blank 的内容在 _top 中打开,但它忽略了 forms

我如何修改扩展以使其也考虑表单,并强制它们也在同一个选项卡中打开?

我已经下载了扩展并查看了代码,主要的js似乎在sametab.js文件中。我在下面包含了这个,我相信我可以以某种方式修改它以满足我的需要。

感谢任何帮助。

"use strict";
// "_blank|_self|_parent|_top|framename"; "framename" should not start with "_"
// - list of iframe names only contains the names of iframes and not the names
// of windows in other tabs that could be targets
// - list of iframe names is not updated if an iframe's name changes

(function() {
   var sameTab = {
      converted: false,
      observer: null,
      iframeNameList: [],
      index: -1,

      mutationObserverFunction: function(mutations, observer) {
         sameTab.observer.disconnect();
         mutations.forEach(function(mutation) {
            var i, node, target;
            target = mutation.target;
            if (!document.contains(target)) return;
            switch (mutation.type) {
            case "attributes":
               if (mutation.attributeName !== "target" ||
                  target.tagName !== "A") return;
               target.onclick = (target.target === "" ||
                  target.target[0] === '_') ? "" : sameTab.doNamedTarget;
               if (target.target.toLowerCase() !== "_blank" ||
                  mutation.oldValue === "_top") return;
               target.target = "_top";
               break;
            case "childList":
               for (i = 0; i < mutation.addedNodes.length; i++) {
                  node = mutation.addedNodes[i];
                  if (node.parentNode !== target || node.nodeType !=
                     document.ELEMENT_NODE) continue;
                  sameTab.convertLinks(node);
               }
               break;
            }
         });
         sameTab.observeDocument();
      },


      observeDocument: function() {
         sameTab.observer.observe(document, {
            childList: true,
            subtree: true,
            characterData: false,
            attributes: true,
            attributeOldValue: true,
            attributeFilter: ["target"]
         });
      },


      convertDocument: function(eventObject) {
         sameTab.convertLinks(document);
         sameTab.observer = new MutationObserver(sameTab.mutationObserverFunction);
         sameTab.observeDocument();
         sameTab.converted = true;
      },

      // When a link with a named target is clicked, change the target to "_top"
      // unless the name is in the list of iframe names.
      doNamedTarget: function(eventObject) {
         // First make sure the iframe's own name is correct.
         if (sameTab.index !== -1) {
            sameTab.iframeNameList[sameTab.index] === window.name;
         }
         // Do nothing if the target name is in the list of window names.
         if (sameTab.iframeNameList.indexOf(eventObject.target.target) !== -1) return;
         eventObject.target.target = "_top";
      },


      // If the link target is "_blank" change it to "_top". If it is a name
      // which does not begin with "_" set the link's click event handler so
      // the list of iframe names can be checked for the target when the link is
      // clicked.
      convertLinks: function(node) {
         var i, linkElements;
         linkElements = node.querySelectorAll("a[target]");
         for (i = 0; i < linkElements.length; i++) {
            if (linkElements[i].target === "") continue;
            if (linkElements[i].target[0] !== "_") {
               linkElements[i].onclick = sameTab.doNamedTarget;
            } else if (linkElements[i].target.toLowerCase() === "_blank") {
               linkElements[i].target = "_top";
            }
         }
         if (node.tagName !== "A" || node.target === "") return;
         if (node.target[0] !== "_") {
            node.onclick = sameTab.doNamedTarget;
         } else if (node.target.toLowerCase() === "_blank") {
            node.target = "_top";
         }
      }
   };


   var frame = null;
   if (window === top) {
      // Top frame
      frame = {
         iframeList: [],
         convertAllLinks: false,
         hostname: null,

         // Delete an item from the list of iframes.
         removeDeletedIframes: function(source) {
            var i;

            for (i = frame.iframeList.length - 1; i >= 0; i--) {
               if (frame.iframeList[i].source && (!source ||
              frame.iframeList[i].source !== source)) continue;
               frame.iframeList.splice(i, 1);
               sameTab.iframeNameList.splice(i, 1);
            }
         },


         sendIframeList: function(source) {
            var i, len, origin;
            // First remove any deleted iframes from the lists.
            frame.removeDeletedIframes(source);
            len = frame.iframeList.length;
            for (i = 0; i < len; i++) {
               origin = (frame.iframeList[i].origin === "null") ?
                  "*" : frame.iframeList[i].origin;
               frame.iframeList[i].source.postMessage({
                  senderId: "sameTabExtensionTop",
                  message: "nameList",
                  iframeNameList: sameTab.iframeNameList,
                  index: i,
               }, origin);
            }
         },


         checkLists: function(items) {
            var i;
            if (!items.settingsInitialized) {
               console.warn("Stored data missing.");
               window.removeEventListener("message", frame.windowMessages, false);
               frame.iframeList.length = 0;
               sameTab.iframeNameList.length = 0;
            } else if (!items.convertLinks ||
               (items.useWhitelist &&
               items.whitelist.indexOf(frame.hostname) == -1) ||
               items.blacklist.indexOf(frame.hostname) != -1) {
               window.removeEventListener("message", frame.windowMessages, false);
               frame.iframeList.length = 0;
               sameTab.iframeNameList.length = 0;
            } else {
               frame.convertAllLinks = true;
               frame.sendIframeList();
               if (document.readyState === "interactive" ||
                  document.readyState === "complete") {
                  sameTab.convertDocument(null);
               } else {
                  document.addEventListener("DOMContentLoaded",
                     sameTab.convertDocument, false);
               }
            }
         },


         getHostname: function() {
            switch (location.protocol) {
            case "file:":
               return "file://" + location.hostname + location.pathname;
               break;
            case "http:":
            case "https:":
               return location.hostname;
               break;
            default:
               return null;
               break;
            }
         },


         windowMessages: function(eventObject) {
            var i, len;
            if (!eventObject.data ||
               eventObject.data.senderId !== "sameTabExtensionIframe") return;
            switch (eventObject.data.message) {
            case "windowUnloaded":
               frame.sendIframeList(eventObject.source);
               break;
            case "contentLoaded":
               if (!eventObject.source ||
                  eventObject.source.top !== window) return;
               len = frame.iframeList.length;
               for (i = 0; i < len; i++) {
                  if (frame.iframeList[i].source === eventObject.source) break;
               }
               frame.iframeList[i] = eventObject;
               sameTab.iframeNameList[i] = eventObject.data.frameName;
               if (!frame.convertAllLinks) return;
               frame.sendIframeList(null);
               break;
            }
         }
      };

      frame.hostname = frame.getHostname();
      if (frame.hostname) {
         window.addEventListener("message", frame.windowMessages, false);
         chrome.storage.local.get(null, frame.checkLists);
      }
   } else {
      // Iframes
      frame = {
         // Accept messages from the top window only.
         windowMessages: function(eventObject) {
            if (eventObject.source !== top) return;
            if (!eventObject.data ||
               eventObject.data.senderId !== "sameTabExtensionTop" ||
               eventObject.data.message !== "nameList" ||
               !eventObject.data.iframeNameList) return;
            sameTab.iframeNameList = eventObject.data.iframeNameList;
            sameTab.index = eventObject.data.index;
            if (sameTab.converted) return;
            sameTab.convertDocument(null);
         },


         // Tell top window that the window has unloaded
         windowUnload: function(eventObject) {
            var origin;
            try {
               origin = top.location.origin;
            } catch(err) {
               origin = "*";
            }
            top.postMessage({
               senderId: "sameTabExtensionIframe",
               message: "windowUnloaded",
            }, origin);
         },


         // Post the window's name to the top window.
         contentLoaded: function(eventObject) {
            var origin;
            try {
               origin = top.location.origin;
            } catch(err) {
               origin = "*";
            }
            top.postMessage({
               senderId: "sameTabExtensionIframe",
               message: "contentLoaded",
               frameName: window.name
            }, origin);
         }
      };

      window.onunload = frame.windowUnload;
      window.addEventListener("message", frame.windowMessages, false);
      document.addEventListener("DOMContentLoaded", frame.contentLoaded, false);
   }
}());

最佳答案

只需在 (function() {

下面的所有表单上添加 _top 属性
var submitButtons = document.getElementsByTagName("button");
for (var i = 0; i < submitButtons .length; i++) {
    submitButtons[i].setAttribute("target", "_top");
}

关于javascript - 强制链接在同一窗口/选项卡中打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33373885/

相关文章:

javascript - 如何从用户输入的可编辑 <td> 中获取文本?

jquery - 如何找到与输入元素最接近且处于同一级别的标签

html - 具有长时间问题的 CSS 过渡宽度/位置

javascript - 防止多次提交文本区域数据

jquery - 用作弹出窗口时,iPad div 内容不滚动

javascript - 使用 jQuery append 样式标签

javascript - 如何使用 jquery 将包含所有元素的数组传递给 php

javascript - JS 如何从 JSON 键中获取值?

javascript - 如何用http请求的响应填充数组?

javascript - 使用特定 ID 标记创建 DIV 的 Javascript 数组