-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshareToken.js
38 lines (32 loc) · 1.36 KB
/
shareToken.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* shareToken -- share CSRF token from session page to extension */
/*jshint undef: true */
/*globals document, chrome */
// console.log('shareToken content script');
// This is a content script invoked via executeScript() from bg.js.
// Chrome content scripts get access to the DOM and to
// the extension via message passing.
// https://developer.chrome.com/extensions/content_scripts
// https://developer.chrome.com/extensions/contentSecurityPolicy#interactions
// https://developer.chrome.com/extensions/messaging
(function(dom, onMessage) {
"use strict";
// "content scripts ... cannot ... use variables or functions
// defined by web pages ..." however "DOM injected scripts that
// would be executed immediately upon injection into the page will
// execute as you might expect."
var share = function(expr, id, attr) {
var scr = dom.createElement('script');
scr.id = id;
scr.textContent = (
'document.getElementById("' + id + '")' +
'.setAttribute("data-share", ' + expr + ')');
(dom.head || dom.documentElement).appendChild(scr);
return scr.getAttribute('data-share');
};
var token = share('_token', 'token-sharing-script');
onMessage.addListener(function(req, sender, sendResponse) {
// console.log('onRequest listener...');
sendResponse(token);
// console.log('sent _token');
});
})(document, chrome.runtime.onMessage);