简体   繁体   English

如何将数据从网页传递到chrome ext?

[英]How pass data from webpage to chrome ext?

I try to pass *auth_token* to my Chrome extention for use it in GET requests further. 我尝试将* auth_token *传递给我的Chrome扩展程序,以在GET请求中进一步使用它。

I think it's good if 我认为这很好

  1. we try to get user from $.get('APP/get/user' {auth_token:''}, callback) [in my Chrome extention] 我们尝试从$ .get('APP / get / user'{auth_token:''},回调)获取用户[在我的Chrome扩展中]
  2. if we got 'not_auth' response, callback open auth_page in new tab [in my Chrome extention] 如果收到“ not_auth”响应,请在新标签页中回调打开auth_page [在我的Chrome扩展中]
  3. we login and redirect to the page, which where generate *auth_token* [in my WEB-APP-PAGE] 我们登录并重定向到页面,在[WEB-APP-PAGE]中生成* auth_token *
  4. pass *auth_token* to my Chrome extention ????? 将* auth_token *传递给我的Chrome扩展范围????? How? 怎么样? via JS? 通过JS? [in my WEB-APP-PAGE] [在我的WEB-APP-PAGE中]

How to realize paragraph 4? 如何实现第4段? thank you 谢谢

Good to apsillers 对射手好

yes, finally, i GET it! 是的,最后,我明白了! in my contentscript.js (which load in my token-page) get the token and send it to background 在我的contentscript.js(加载到我的令牌页面中)中获取令牌并将其发送到后台

contentscript.js contentscript.js

$(function(){
  //get token from page
  var token = $.getUrlVar('token');
  if (typeof token != 'undefined') {
    chrome.extension.sendMessage({token: token});
  }
});

background.js background.js

/*button handler*/
function main_click() {
  alert(localStorage['auth_token']);
}
chrome.browserAction.onClicked.addListener(main_click);


/*listener*/
chrome.extension.onMessage.addListener(
  function(request, sender, sendResponse) {
    if (request.token)
      localStorage['auth_token'] = request.token;
  });

I think localStorage of page could act as a brigde between pages and extension. 我认为页面的localStorage可以充当页面和扩展之间的桥梁。

At stage 3 where the auth_token created, dump it to localStorage , 在创建auth_token的第3阶段,将其转储到localStorage

<script type="text/javascript">
 ...
 var auth_token = "sfafsafasfsaf";
 localStorage["auth_token"] = auth_token;
 ...
</script>

And get the auth_token in content script (content.js), 并在内容脚本 (content.js)中获得auth_token,

console.log(localStorage["auth_token"])

The Chrome documentation on content script communication recommends using window.postMessage in the sending code (here, the web page) and using window.addEventListener("message", ...) in the listening code (here, the content script of the Chrome extension, injected into the page). Chrome上有关内容脚本通信的文档建议在发送代码(在此为网页)中使用window.postMessage ,在侦听代码(在此为Chrome的内容脚本window.addEventListener("message", ...)中使用window.addEventListener("message", ...)扩展名,插入到页面中)。 Technically, any kind of custom DOM event could also do this, but postMessage / message already has built-in support. 从技术上讲,任何类型的自定义DOM事件也可以做到这一点,但是postMessage / message已经具有内置支持。

You should be able to lift the example code from the codes nearly verbatim: 您应该能够从几乎所有的代码中提起示例代码:

Native web page: 本机网页:

// right after we get auth_token saved to a variable...
window.postMessage({ auth_token: auth_token }, "http://www.mypagedomain.com");

(Make sure http://www.mypagedomain.com is changed to your actual protocol/domain.) (确保将http://www.mypagedomain.com更改为您的实际协议/域。)

contentscript.js (in Chrome extension, listening) contentscript.js (在Chrome扩展程序中,正在侦听)

window.addEventListener("message", function(event) {
    // We only accept messages from ourselves
    if (event.source != window) {
      return;
    }

    console.log("auth_token received: " + event.data.auth_token);
}, false);

From inside the event listener, you could use message passing to pass the auth_token to your background page, if necessary. 在事件侦听器内部,如有必要,您可以使用消息传递auth_token传递到您的后台页面。

EDIT: 编辑:

Your manifest should include something like this (note the use of run_at below to inject the script before the page loads): 您的清单中应包含以下内容(请注意,在页面加载之前,请使用以下run_at注入脚本):

...
"content_scripts": [
  {
    "matches": ["http://www.mypagedomain.com/*"],
    "js": ["contentscript.js"],
    "run_at": "document_start"
  }
],
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM