Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var localGl = {
- glType : 'function',
- appName : 'mutexTest',
- glHash : '',
- clientId: '0',
- glKey: '',
- createRandomElementId : function(salt){
- var result = 'i';
- var key = parseInt(this.clientId);
- key = key + 'x' + key;
- for( var i = 0; i < key.length; i++){
- var x = parseInt(key[i]);
- if( i < salt.length ){
- var charc = salt[i];
- x += charc.charCodeAt(0) % 25;
- }
- if( x < 25 ){
- charc = String.fromCharCode(97+x);
- result += charc;
- }
- }
- return result;
- },
- pbComputeHash : function(s){
- return s.split("").reduce(function(a,b){a=((a<<5)-a)+b.charCodeAt(0);return (a&a)>>>1},0);
- },
- localScope : function(div, input, key){
- var attr = '(' + this.glType + '(){' + input;
- var value = attr + '})();'
- div.setAttribute(key, value);
- }
- }
- self.port.on('insertWarning', function(params){
- var warning = params.tokens;
- var res = insertWarning(warning, 0);
- });
- self.port.on('closeWarning', function(params){
- if( params.button ){
- var buttonId = params.button;
- var button = document.getElementById(buttonId);
- if( button ){
- button.click();
- }
- }
- if( params.navigate ){
- document.location.href = params.navigate;
- }
- });
- function registerDocumentMutex(tokens, tries){
- tries = (typeof tries !== typeof undefined) ? tries : 0;
- var maxTries = 40;
- var retryInterval = 500;
- if( tries > maxTries ){
- return false;
- }
- tries++;
- if( tries > maxTries ){
- return false;
- }
- if( !document ){
- setTimeout( function(){ registerDocumentMutex(tokens, tries); }, retryInterval );
- return false;
- }
- var doc = document;
- var readyState = doc.readyState;
- var body = doc.body;
- if( !body ){
- setTimeout( function(){ registerDocumentMutex(tokens, tries); }, retryInterval );
- return false;
- }
- /* create a wrapper for saving our mutexes */
- var sWrapperId = localGl.createRandomElementId('mutexWrapper');
- var mWrapper = doc.getElementById(sWrapperId);
- if( !mWrapper ){
- mWrapper = doc.createElement('div');
- mWrapper.id = sWrapperId;
- mWrapper.setAttribute('style', 'display: none;');
- body.appendChild(mWrapper);
- }
- var mutexIsPresentButNotActive = false;
- var className = 'mutex';
- var mCol = document.getElementsByClassName(className);
- if( mCol && mCol.length > 0 ){
- for (var i = 0; i < mCol.length; i++) {
- var el = mCol[i];
- var content = el.textContent;
- if( content.indexOf('/mutex') > 0 ){
- mutexIsPresentButNotActive = true;
- return true;
- }
- }
- }
- var key = localGl.createRandomElementId(tokens);
- var docMutexDiv = doc.createElement('div');
- docMutexDiv.id = key;
- var className = 'mutex';
- docMutexDiv.className=className;
- var checkContent = 'mutex'+'/'+key+'/'+'mutex';
- docMutexDiv.textContent = checkContent;
- docMutexDiv.setAttribute('origin', localGl.appName);
- docMutexDiv.setAttribute('style', 'display: none;');
- mWrapper.appendChild(docMutexDiv);
- return true;
- }
- function mutexIsOff(){
- var canProceed = false;
- var className = 'mutex';
- var mCol = document.getElementsByClassName(className);
- var key = localGl.createRandomElementId(localGl.appName);
- var identifier = 'stop'+'/'+key+'/'+localGl.appName;
- if( mCol && mCol.length > 0 ){
- for (var i = 0; i < mCol.length; i++) {
- var el = mCol[i];
- var content = el.textContent;
- if( content.indexOf('/mutex') > 0 ){
- el.textContent = identifier;
- canProceed = true;
- }
- else if( content == identifier ){
- canProceed = true;
- }
- else {
- var attribute = localGl.appName;
- var requestedBy = el.getAttribute('req');
- if( requestedBy ){
- if( requestedBy.indexOf(attribute) == -1 ){
- attribute = attribute + ' ' + requestedBy;
- }
- }
- else{
- requestedBy = attribute;
- }
- el.setAttribute('req', requestedBy);
- }
- }
- }
- return canProceed;
- }
- function insertWarning(warning, tries){
- var url = document.location.href;
- tries = (typeof tries !== 'undefined') ? tries : 0;
- var retryInterval = 100;
- var maxTries = 40;
- tries++;
- if( tries > 40 ){
- /* if you tried too many times, give it up */
- return;
- }
- var warningDone = false;
- var body = document.getElementsByTagName('body')[0];
- if( (document.readyState != 'loading' && document.readyState != 'interactive') || tries >= 10 || body ){
- /* heuristic approach: give your best last times (>=10), even if the document hasn't completely loaded */
- if( !body ){
- /* Because there is no body, but just frames */
- var frameCount = window.frames.length;
- if( frameCount > 0 ){
- if (top.location == location){
- self.port.emit('insertWarning', tries);
- }
- /* append to each frame, there is no way of knowing which one is visible */
- for( var i = 0; i < frameCount; i++){
- var doc = window.frames[i].document;
- var frameHost = doc.location.host;
- var docBody = doc.body;
- if( docBody ){
- var warningModal = doc.createElement('script');
- warningModal.textContent = warning;
- docBody.appendChild(warningModal);
- warningDone = true;
- return true;
- }
- else{
- setTimeout(function(){
- var docBody = doc.body;
- if( docBody ){
- docBody.appendChild(warningModal);
- }
- }, 3000);
- }
- }
- }
- else{
- }
- }
- else{
- /* body present, put append it there */
- var warningModal = document.createElement('script');
- warningModal.textContent = warning;
- body.appendChild(warningModal);
- warningDone = true;
- return true;
- }
- }
- /* if for some reason the document is not ready, try it again later */
- if( !warningDone ){
- var interval = retryInterval*tries;
- setTimeout(function(){
- insertWarning(warning, tries);
- }, interval);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement