Help with JS
-
I use the extension Link Controller (No longer in Chrome Store) and it works perfect to define if the links in a page are opened in the same tab or in a new one.
The problem occurs on the Vivaldi forum page with this setting:
After scrolling some pages it loses the "new tab" setting and becomes "same tab".
These are the JS codes used by the extension:
background.js:
function tabs(tabId) { chrome.tabs.executeScript(tabId, { file: "inject.js", allFrames: true }, function(result) { if(result != undefined) { chrome.storage.local.get({ global: "_blank"}, function(items) { chrome.tabs.sendMessage(tabId, items.global) if(items.global == "_blank") { chrome.browserAction.setIcon({ path: "N.png", tabId: tabId }) chrome.browserAction.setTitle({ title: "New", tabId: tabId }) } else if(items.global == "_self") { chrome.browserAction.setIcon({ path: "C.png", tabId: tabId }) chrome.browserAction.setTitle({ title: "Current", tabId: tabId }) } }) } }) } chrome.runtime.onStartup.addListener(function() { chrome.storage.local.get(null, function(items) { if(items.global == undefined) { chrome.permissions.remove({ origins: ["<all_urls>"]}) } }) }) window.addEventListener("load", function() { chrome.permissions.contains({ origins: ["<all_urls>"]}, function(result) { if(result == true) { chrome.tabs.onUpdated.addListener(tabs) } }) }) chrome.permissions.onAdded.addListener(function() { chrome.tabs.onUpdated.addListener(tabs) chrome.tabs.query({}, function(result) { for(var tab in result) { tabs(result[tab].id) } }) }) chrome.permissions.onRemoved.addListener(function() { chrome.tabs.onUpdated.removeListener(tabs) })
inject.js:
chrome.runtime.onMessage.addListener(function(message) { var links = document.querySelectorAll("a[href]") for(var link in links) { links[link].target = message } })
popup.js:
document.body.addEventListener("click", function(event) { if(event.target.tagName == "BUTTON") { if(event.target.parentElement.id == "tab") { chrome.tabs.executeScript({ file: "inject.js", allFrames: true }, function(result) { if(result != undefined) { chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) { chrome.tabs.sendMessage(tabs[0].id, event.target.className) chrome.browserAction.setIcon({ path: event.target.textContent[0] + ".png", tabId: tabs[0].id }) chrome.browserAction.setTitle({ title: event.target.textContent, tabId: tabs[0].id }) }) } }) } else if(event.target.className == "initial") { chrome.permissions.remove({ origins: ["<all_urls>"]}) chrome.storage.local.clear() } else { chrome.permissions.request({ origins: ["<all_urls>"]}, function(granted) { if(granted == true) { chrome.storage.local.set({ global: event.target.className }) } }) } } })
Could someone knowledgeable about Javascript take a look and see if with a simple change the setting can be kept independent of the scroll?