-1

I have 2 tab opened in firefox. I need the following thing.

If the following word is found in the 2nd tab content, then the userscript will close the 2nd tab and then reload the 1st tab.

Sorry, this page isn't available.

I have tried but it is closing the 2nd tab but not reloading the 1st tab.

// ==UserScript==
// @name        Ins Sorry
// @namespace   Ins Sorry
// @version     1
// @include         https://instagram.com/*
// @match      https://instagram.com/*
// ==/UserScript==
if (
  (
    document.documentElement.textContent || document.documentElement.innerText
  ).indexOf('Sorry') > -1
) {

location.reload();
window.top.close();
}

1 Answers1

0

An userscript does not have global access to your internet browser, it knows only the page on which it was called and knows absolutely nothing about the other tabs. Its action is limited to the context of the page and the tab on which it executes.

This is why you can not ask your script to update the first tab and close the second.

What happens using your code:

location.reload(); // Try to reload the current tab (the 2nd one)
window.top.close(); // Close the current tab (the 2nd one)

However, usesrcript have access to a shared memory space of your browser called Web Storage. You can write content in this space with a script that runs on the second tab, and the script of the first tab can read this content.

What is useful is that there is an event that triggers when this memory space is changed.

See this related answer: Javascript; communication between tabs/windows with same origin

So you can attach a handler to this event on your script which runs in the first tab, and close the tab when a value is changed.

2nd tab userscript:

if ((document.documentElement.textContent || document.documentElement.innerText).indexOf('Sorry') > -1) {
    sessionStorage.setItem("word_found", Date.now());
    window.top.close();
}

1st tab userscript:

window.addEventListener("storage", function(event) {
   if (event.key == "word_found") {
       location.reload();
   }
});
Community
  • 1
  • 1
Delgan
  • 16,542
  • 9
  • 83
  • 127
  • Do i need to add both 1st tab and 2nd tab userscript in one script? or in separate script? Sorry for asking, i don't know too much. – Barkat Ullah Jun 26 '15 at 15:25
  • Also if possible in imacros iim, then also let me know please. I can switch between tab in imacros iim but it was not working as i said. – Barkat Ullah Jun 26 '15 at 15:34
  • @BarkatUllah It depends on the rest of your script and its purpose. The script must act differently depending whether it is in the situation 1 or 2. You can probably do it in one script, differentiating using a `if`. If the tabs relate to two different sites, you can also make two userscripts. – Delgan Jun 26 '15 at 15:37
  • @BarkatUllah I do not know too much about iMacro, sorry. – Delgan Jun 26 '15 at 15:37
  • I tried using single and also using 2 separate scripts, but its only closing the 2nd tab, but not reloading 1st tab – Barkat Ullah Jun 26 '15 at 15:43
  • @BarkatUllah I can not code for yourself. I can not help you more. I know nothing about your script. – Delgan Jun 26 '15 at 16:04