我正在编写一个注入网页的用户脚本。该脚本从网络服务器读取一些数据,我想将消息发送到侦听应用程序以对数据做出反应。
现在,我所做的就是尝试向我的监听应用程序发送一个字符串命令,看看我是否可以读取它。我的代码在注入之前可以工作,但之后我收到“未定义的引用错误”。
我怀疑这与此有关“油猴访问违规” http://wiki.greasespot.net/0.7.20080121.0_compatibility。但是,我一直无法找到有效的解决方案。我正在 Chrome 中开发。
这是我无法开始工作的代码部分。
GM_xmlhttpRequest({
method: "POST",
url: "http://localhost:7777",
data: "testing123",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
onload: function(response)
{
if (response.responseText.indexOf("TEST") > -1)
{
console.log("Response confirmed...");
}
}
});
我对脚本编写还很陌生,所以也许我遗漏了一些明显的东西。我如何让它在注入的代码中工作?
GM_
函数在注入代码中不起作用,因为注入代码在目标页面的范围内运行。如果他们did在那里工作,那么无良网站也可以使用GM_
功能——做不可言喻的恶事。
解决方案,首先是最优选的:
-
不要注入代码。很多时候,这确实没有必要,而且总是会让事情变得复杂。仅当您确实需要使用目标页面加载的某些 javascript 时才注入代码。
对于像 jQuery 这样的库,您将获得更好的性能@require
指令 (Firefox),或粘贴库代码或使用一个习俗清单.json file https://stackoverflow.com/a/5259212/331508包含它(Chrome)。
通过不注入代码,您可以:
- 保持轻松使用的能力
GM_
功能
- 避免或减少对外部服务器交付库的依赖。
- 避免潜在的副作用以及页面 JS 的依赖性。 (你甚至可以使用类似的东西NoScript完全禁用页面的 JS,而您的脚本仍然运行。)
- 防止恶意网站利用您的脚本来访问
GM_
功能。
Use 篡改猴扩展 https://chrome.google.com/extensions/detail/dhdgffkkebhmkfjojejmpbldmpobfkfo(铬合金)。这允许您通过提供更好的 Greasemonkey 模拟来避免脚本注入。您可以使用@require
指令和更强大/有风险的版本unsafeWindow
比 Chrome 本身提供的更多。
将您的用户脚本代码拆分为注入的部分——这些部分无法使用GM_
功能——以及非注塑部件。使用消息传递 http://code.google.com/chrome/extensions/content_scripts.html#host-page-communication、轮询和/或特定 DOM 节点以在范围之间进行通信。
如果您确实必须使用注入代码,这里有一个示例脚本,展示了如何执行此操作:
// ==UserScript==
// @name _Fire GM_ function from injected code
// @include https://stackoverflow.com/*
// @grant GM_xmlhttpRequest
// ==/UserScript==
/* Warning: Using @match versus @include can kill the Cross-domain ability of
GM_xmlhttpRequest! Bug?
*/
function InjectDemoCode ($) {
$("body").prepend ('<button id="gmCommDemo">Open the console and then click me.</button>');
$("#gmCommDemo").click ( function () {
//--- This next value could be from the page's or the injected-code's JS.
var fetchURL = "http://www.google.com/";
//--- Tag the message, in case there's more than one type flying about...
var messageTxt = JSON.stringify (["fetchURL", fetchURL])
window.postMessage (messageTxt, "*");
console.log ("Posting message");
} );
}
withPages_jQuery (InjectDemoCode);
//--- This code listens for the right kind of message and calls GM_xmlhttpRequest.
window.addEventListener ("message", receiveMessage, false);
function receiveMessage (event) {
var messageJSON;
try {
messageJSON = JSON.parse (event.data);
}
catch (zError) {
// Do nothing
}
console.log ("messageJSON:", messageJSON);
if ( ! messageJSON) return; //-- Message is not for us.
if (messageJSON[0] == "fetchURL") {
var fetchURL = messageJSON[1];
GM_xmlhttpRequest ( {
method: 'GET',
url: fetchURL,
onload: function (responseDetails) {
// DO ALL RESPONSE PROCESSING HERE...
console.log (
"GM_xmlhttpRequest() response is:\n",
responseDetails.responseText.substring (0, 80) + '...'
);
}
} );
}
}
function withPages_jQuery (NAMED_FunctionToRun) {
//--- Use named functions for clarity and debugging...
var funcText = NAMED_FunctionToRun.toString ();
var funcName = funcText.replace (/^function\s+(\w+)\s*\((.|\n|\r)+$/, "$1");
var script = document.createElement ("script");
script.textContent = funcText + "\n\n";
script.textContent += 'jQuery(document).ready( function () {' + funcName + '(jQuery);} );';
document.body.appendChild (script);
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)