mercredi 24 février 2016

Chrome Extension SOS

Can anybody help us out? We have created a extension to search a text box for key words and if a keyword is found we then want to write text into another text box. We aren't sure how to create a second send response (or even if it is a send response we need) within the content script or how to access it in the background script. Code listed below.

Content Script:

// Listen for messages
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
console.log(msg);

// If the received message has the expected format...
if (msg.text === 'report_back') 
{
    // Call the specified callback, passing
    // the web-page's DOM content as argument

    sendResponse(document.getElementById('.........').innerHTML);
} 

});

Background Script:

var urlRegex = /^https?:\/\/(?:[^./?#]+\.)?stackoverflow\.com/;

// A function to use as callback
function doStuffWithDom(domContent) {

var search = false;

if (domContent.match(/......./gi))
{
    window.alert('......');
}
else
{
    var r = confirm("Search indicates no tasks listed!");
        if (r == true) {

            //Type Text Code

        } else {

            x = "You pressed Cancel!"; //We are aware this does not do anything
        }
}

}

// When the browser-action button is clicked...
chrome.browserAction.onClicked.addListener(function (tab) {

 // ...check the URL of the active tab against our pattern and...
    // ...if it matches, send a message specifying a callback too

    chrome.tabs.sendMessage(tab.id, {text: 'report_back'}, doStuffWithDom);

});

Manifest:

{
"manifest_version": 2,
"name": "Test Extension",
"version": "0.0",


 "background": {
 "persistent": false,
 "scripts": ["background.js"]
 },
  "content_scripts": [{
  "matches": ["*://*.com/*"],
 "js": ["content.js"]
  }],
  "browser_action": {
  "default_title": "Test Extension"
   },

   "permissions": ["activeTab"]
   }

Aucun commentaire:

Enregistrer un commentaire