当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Chrome Extension Messaging help

问题描述:

Heres the code: http://pastebin.com/UxhJAdMb

I want to send the getlink.js (chrome content script) variable "found" to a textbox in popup.html

I dont understand the documentation on message passing here: http://code.google.com/chrome/extensions/messaging.html (new to javascript)

Also, how can I modify this Regex to find all sites staring with like wwww.stackoveflow or http://www.stackoverflow.com

var reg = /(https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?)/g

网友答案:

Chrome messaging is a little confusing at first, but your listener is in the wrong place. You need to first send a request from your contentscript, which in your case is getlink.js

# contentscript (getlink.js)
// ... code that does stuff here ...

chrome.extension.sendRequest({greeting: 'hello'}, function(response) {
  console.log(response);
});

Then inside your popup.html you can add your listener:

# popup.html
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {

  // do something with request

  // always send something back, even if it's empty
  sendResponse({});
});
分享给朋友:
您可能感兴趣的文章:
随机阅读: