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

Javascript 强制链接在同一窗口/选项卡中打开,javascript,jquery,html,google-chrome,kiosk,Javascript,Jquery,Html,Google Chrome,Kiosk,我正在使用谷歌浏览器的这个扩展- 它非常简单,基本上按照它所说的做,强制所有链接在同一个窗口/选项卡中打开。我需要此功能用于触摸屏信息亭左/右滑动导航 当涉及到链接时,该插件工作得非常好。但是,在“提交”web搜索表单中执行搜索时,它不起作用。大概是因为扩展正在强制在\u top中打开任何带有\u blank的内容,但它忽略了表单 如何修改扩展,使其同时考虑表单,并强制在同一选项卡中打开表单 我已经下载了扩展名并查看了代码,主js似乎在sametab.js文件中。我已经把它包括在下面,我相信我可

我正在使用谷歌浏览器的这个扩展-

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

当涉及到链接时,该插件工作得非常好。但是,在“提交”web搜索表单中执行搜索时,它不起作用。大概是因为扩展正在强制在
\u top
中打开任何带有
\u blank
的内容,但它忽略了
表单

如何修改扩展,使其同时考虑表单,并强制在同一选项卡中打开表单

我已经下载了扩展名并查看了代码,主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);
   }
}());
“严格使用”;
//“_blank | u self | u parent | u top | framename”;“framename”不应以“\u1”开头
//-iframe名称列表仅包含iframe的名称,而不包含iframe的名称
//可能成为目标的其他选项卡中的窗口数
//-如果iframe的名称发生更改,则不会更新iframe名称列表
(功能(){
var sameTab={
转换:错误,
观察员:空,
iframeNameList:[],
索引:-1,
突变观察者功能:功能(突变,观察者){
sameTab.observer.disconnect();
突变。forEach(功能(突变){
变量i,节点,目标;
target=mutation.target;
如果(!document.contains(target))返回;
开关(突变型){
案例“属性”:
if(mutation.attributeName!=“target”||
target.tagName!=“A”)返回;
target.onclick=(target.target==“”||
target.target[0]==''.'?'':sameTab.doNamedTarget;
if(target.target.toLowerCase()!=“\u blank”||
mutation.oldValue==“\u top”)返回;
target.target=“_top”;
打破
案例“儿童名单”:
对于(i=0;i=0;i--){
if(frame.iframeList[i].source&(!source)||
frame.iframeList[i].source!==source))继续;
框架iframeList拼接(i,1);
sameTab.iframeNameList.拼接(i,1);
}
},
sendIframeList:函数(源){
变量i,len,来源;
//首先从列表中删除所有已删除的iFrame。
帧。删除的帧(源);
len=frame.iframeList.length;
对于(i=0;ivar submitButtons = document.getElementsByTagName("button");
for (var i = 0; i < submitButtons .length; i++) {
    submitButtons[i].setAttribute("target", "_top");
}