A
A
Anickdot2016-12-24 19:51:02
JavaScript
Anickdot, 2016-12-24 19:51:02

How to make one script execute synchronously in different browser tabs?

To work, I need a script-timer that works on VK pages, which would be updated every time a certain key combination is pressed. Did business in tampermanki.

1) I created the timer itself: 2) Added two functions to make it work and update:<p id="data-time"> 360 </p>

function timerito() {
        var elem=document.getElementById('data-time');
        if(elem.innerHTML!=0) {
          elem.innerHTML--;
          setTimeout(timerito, 1000);
        }
        else alert('Пора делать SMM!');
      }
function timerback() {
        var elem=document.getElementById('data-time');
        if(elem.innerHTML!=360) {
          elem.innerHTML=360;
        }			
      }


3) made the event handler and the start of the timer:
var altIsPressed = false; 
    $(window).keydown(function(event){
    if(event.keyCode == 18) {
        altIsPressed = true; event.preventDefault();
    }
}); 
    window.onkeydown = function(event) {
     if (altIsPressed && event.keyCode === 67) {
        event.preventDefault();  timerback(); 
     }
}
setTimeout(timerito, 1000);


Everything works great. The timer is located under the left menu, is updated when you press alt + c, starts when the page is opened. But I would really like it to be synchronized on different tabs. Is it even possible to do this?
That is, if my timer reached 100 seconds, then in a new tab it would start not from 360, but from the same 100 seconds. And if I update the timer in a new tab, then it would be updated in the old one.

I don't even know how to approach this case, tell me, please.

Is it possible to implement this case as a Chrome extension? In order not to become attached to VK itself and tabs

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DeNIZka, 2016-12-26
@Anickdot

With a chrome extension, there shouldn't be any problems at all.
You insert the Content script only on VK pages (well, or on everything if you need it) . You
use the background page as a signaling mechanism for synchronizing timers.
In general, you can use the timer only on the background page, and through the content script, insert a display on the page.
In the Documentation , everything is described in detail

N
Nurlan, 2016-12-26
@daager

localStorage?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question