javascript - Force link to open in same window/tab -
i using google chrome extension - open link in same tab.
it's simple , says, forces links open in same window/tab. require functionality touch screen kiosk left/right swipe navigation.
the plugin works when comes links. doesn't work when search performed in 'submit' web search form. presumably because extension forcing _blank
open in _top
, it's ignoring forms
.
how can modify extension in such way takes consideration forms, , forces these open in same tab?
i have downloaded extension , viewed code, main js seems in sametab.js
file. have included below, i'm sure can modify somehow fit needs.
any appreciated.
"use strict"; // "_blank|_self|_parent|_top|framename"; "framename" should not start "_" // - list of iframe names contains names of iframes , not names // of windows in other tabs targets // - list of iframe names not updated if 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": (i = 0; < 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 link named target clicked, change target "_top" // unless name in list of iframe names. donamedtarget: function(eventobject) { // first make sure iframe's own name correct. if (sametab.index !== -1) { sametab.iframenamelist[sametab.index] === window.name; } // nothing if target name in list of window names. if (sametab.iframenamelist.indexof(eventobject.target.target) !== -1) return; eventobject.target.target = "_top"; }, // if link target "_blank" change "_top". if name // not begin "_" set link's click event handler // list of iframe names can checked target when link // clicked. convertlinks: function(node) { var i, linkelements; linkelements = node.queryselectorall("a[target]"); (i = 0; < 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 item list of iframes. removedeletediframes: function(source) { var i; (i = frame.iframelist.length - 1; >= 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 deleted iframes lists. frame.removedeletediframes(source); len = frame.iframelist.length; (i = 0; < 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; (i = 0; < 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 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 window has unloaded windowunload: function(eventobject) { var origin; try { origin = top.location.origin; } catch(err) { origin = "*"; } top.postmessage({ senderid: "sametabextensioniframe", message: "windowunloaded", }, origin); }, // post window's name 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); } }());
you can try setting queryselectorall forms well. i'm pretty sure forms support target
.
Comments
Post a Comment