开发者

Chrome Extension Messaging help

开发者 https://www.devze.com 2023-03-01 18:27 出处:网络
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

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: ht开发者_Python百科tp://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({});
});
0

精彩评论

暂无评论...
验证码 换一张
取 消