I set up a server on http://localhost:8080 where http://example.com can do POST requests :
(我在http:// localhost:8080上设置了服务器,其中http://example.com可以执行POST请求:)
'use strict';
const express = require('express');
const app = express();
const port = 8080;
// allowing CORS for example.com
app.use('/', function (req, res, next) {
res.header('Access-Control-Allow-Origin', 'http://example.com');
if (req.method === 'OPTIONS') {
res.header('Access-Control-Allow-Methods', 'OPTIONS, POST');
res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length');
res.status(200).send();
} else {
next();
}
});
// handling POST requests
app.use('/', function (req, res) {
console.log('a client did a POST request');
res.status(200);
});
app.listen(port, () => console.log ('server started on port ' + port));
It works fine : I can't do a POST request to http://localhost:8080 from http://localhost:8081 because of the same origin policy.
(它工作正常:由于相同的原始策略,我无法从http:// localhost:8081向http:// localhost:8080发出POST请求。)
Then I wrote a web extension for Firefox that will try to do a POST request to http://localhost:8080 from any domain.
(然后,我为Firefox编写了一个Web扩展程序,它将尝试从任何域向http:// localhost:8080发出POST请求。)
Here is its manifest :
(这是它的清单:)
{
"manifest_version" : 2,
"name" : "aBasicExtension",
"version" : "0.0.0",
"content_scripts" : [
{
"matches" : ["<all_urls>"],
"js" : ["content-script.js"]
}
],
"permissions" : ["*://*.localhost/*"]
}
and its content-script.js
code :
(及其content-script.js
代码:)
(() => {
'use strict';
const xhr = new XMLHttpRequest();
xhr.open('POST', 'http://localhost:8080');
xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
xhr.addEventListener('readystatechange', () => {
if (xhr.readyState === XMLHttpRequest.DONE){
if (xhr.status === 200) console.log('OK');
else console.error('an error has occured : ' + xhr.status);
}
});
xhr.send(JSON.stringify({dataName: 'some data here'}));
})();
What I don't understand is that it works.
(我不明白的是它有效。)
The extension do the request to http://localhost:8080 and Firefox didn't block it because the manifest allows it, however the server ( http://locahost:8080 ) didn't give his permission .(该扩展程序将请求发送到http:// localhost:8080 ,Firefox并未阻止它,因为清单允许它,但是服务器( http:// locahost:8080 ) 没有给予他许可 。)
ask by Cl00e9ment translate from so