2016-03-24 23 views
0

Kullanıcı, veritabanımda saklanan URL'yi ziyaret ederse DIV içerikli mesaj oluşturmak istiyorum. Aslında, bir uyarı mesajında ​​çalışıyor ancak DIV istiyorum, bu yüzden sayfanın DOM'sine contentcript.js ile erişmeliyim ve bu işe yaramalıyım ama fonksiyonun contentcript.js'de çalışmasını nasıl engelleyebilirim çünkü bu işlev, URL'yi background.js'de kontrol ettim!Chrome uzantısı: contentcript.js dosyasındaki işlev background.js den

Sekme hakkında bilgi okuyorum ancak yalnızca kullanıcı bir tarayıcı eyleminin simgesini tıklattığında çalıştırılıyor ve benim durumumda kullanıcı yalnızca tarama yaptığı herhangi bir simgeyi tıklamıyor.

contentscript.js

chrome.runtime.sendMessage(window.location.href); 
var pathname = window.location.pathname; // Returns path only 
var url  = window.location.href; 
//document.getElementsByTagName('title')[0].innerText 

function MessageBox(){ 
    document.documentElement.style.height = '100%'; 
    document.body.style.height = '100%'; 
    document.documentElement.style.width = '100%'; 
    document.body.style.width = '100%'; 

    var div = document.createElement('div'); 
    var btnForm = document.createElement('form'); 
    var btn = document.createElement('input'); 

    //append all elements 
    document.body.appendChild(div); 
    div.appendChild(btnForm); 
    btnForm.appendChild(btn); 
    //set attributes for div 
    div.id = 'myDivId'; 
    div.style.position = 'fixed'; 
    div.style.top = '80%'; 
    div.style.left = '77%'; 
    div.style.width = '22%'; 
    div.style.height = '17%'; 
    div.style.backgroundColor = 'white'; 
    div.style.border='1px solid red'; 

    //set attributes for btnForm 
    btnForm.action = ''; 

    //set attributes for btn 
    //"btn.removeAttribute('style'); 
    btn.type = 'button'; 
    btn.value = 'X'; 
    btn.style.position = 'reltive'; 
    btn.style.top = '10%'; 
    btn.style.left = '80%'; 
    btn.style.width = '5%'; 
    btn.style.height = '3%'; 
} 

background.js

chrome.runtime.onMessage.addListener(function(response, sender, sendResponse) { 

    $.ajax({ 
     type: 'GET', 
     url: 'http://mywebsite.com/api/route', 
     dataType: 'json', 
    }).success(function(results) { 
     for (var i = 0; i < results.length; i++) { 
      if (results[i].url === response) { 
       alert("This page in my database"); // this work but I don't want an alert message 
      } //if 
     } //for 
    }); 
}); 

cevap

0

chrome.runtime.sendMessage asenkron bir yöntemdir.

chrome.runtime.onMessage.addListener(function(response, sender, sendResponse) { 

    $.ajax({ 
     type: 'GET', 
     url: 'http://mywebsite.com/api/route', 
     dataType: 'json', 
    }).success(function(results) { 
     for (var i = 0; i < results.length; i++) { 
      if (results[i].url === response) { 
       sendResponse(true); 
       return; 
      } 
     } 

     sendResponse(false); 
    }); 

    return true; // tell chrome about runnning async job 
}); 

Ve İçerik komut bu

chrome.runtime.sendMessage(window.location.href, function(result) { 
    if (result) { 
     // Found 
     MessageBox(); 
    } 
}); 

hakkında daha fazlasını okuyun yapın: Eğer arka plan komut dosyasında işinizi

bitirdiğinde bunu geri arama yöntemi ve arka plan komut çağrısı sendResponse geçirebilirsiniz Burada geçen mesaj https://developer.chrome.com/extensions/messaging

+0

Teşekkür ederim işte – Seham

İlgili konular