如果我在 Chrome 扩展中创建了一堆函数,是否可以通过控制台访问它们?
例如:
内容脚本.js
function test() {
console.log('hello')
}
Run Code Online (Sandbox Code Playgroud)
然后就可以test()在控制台运行了
我制作了一个 firefox 扩展来获取所有请求 url 并显示它们。但只有当我将代码粘贴到控制台中时,该代码才有效。
当扩展加载时,它没有显示任何错误,看起来它只是无法运行
这是完整的代码
xhrScript.js
(function(){
const proxiedOpen = XMLHttpRequest.prototype.open;
window.XMLHttpRequest.prototype.open = function ( _, url) {
this.__URL = url;
return proxiedOpen.apply(this, arguments);
};
const proxiedSend = window.XMLHttpRequest.prototype.send;
window.XMLHttpRequest.prototype.send = function () {
const { protocol, host } = window.location;
// showing only when it paste in console
console.log("full request url ", `${protocol}//${host}${this.__URL}`);
return proxiedSend.apply(this, [].slice.call(arguments));
};
})();
// this works all times
document.body.style.border = "7px solid blue";
Run Code Online (Sandbox Code Playgroud)
清单.json
{
"manifest_version": 2,
"name": "XHR request urls",
"version": …Run Code Online (Sandbox Code Playgroud) javascript firefox xmlhttprequest firefox-addon firefox-addon-webextensions
当 Chrome 扩展程序尝试通过 chrome.executeScript 在 window 对象中获取自定义函数时,它什么也得不到。
例如:
标签 ID:150
标签js:
window.customfunc = function(){return 'yeap';}
Run Code Online (Sandbox Code Playgroud)
扩展的后台JS:
chrome.tabs.executeScript(150, { code: "console.log(window);" })
Run Code Online (Sandbox Code Playgroud)
清单.json:
{
"background": {
"scripts": [ "background.js" ]
},
"content_scripts": [ {
"exclude_globs": [ ],
"exclude_matches": [ ],
"include_globs": [ "*://*/*" ],
"js": [ "script.js" ],
"matches": [ "http://*/*" ],
"run_at": "document_idle"
} ],
"content_security_policy": "script-src 'self' https://ssl.google-analytics.com; object-src 'self'",
"description": "Test",
"manifest_version": 2,
"name": "Workspace",
"permissions": [ "unlimitedStorage", "notifications", "clipboardWrite", "notifications", "clipboardRead", "management", "tabs", "history", "cookies", "idle", "storage", …Run Code Online (Sandbox Code Playgroud) 我正在编写一个谷歌浏览器扩展程序,并尝试从注入网页的一段代码中将信息发送到我的内容脚本.
根据http://developer.chrome.com/extensions/messaging#external-webpage,我应该使用类似的东西:
// The ID of the extension we want to talk to.
var editorExtensionId = "abcdefghijklmnoabcdefhijklmnoabc";
// Make a simple request:
chrome.runtime.sendMessage(editorExtensionId, {openUrlInEditor: url},
function(response) {
if (!response.success)
handleError(url);
});
Run Code Online (Sandbox Code Playgroud)
问题是,当我这样做时:
var script_code = "var msg = {message : 'plop'};\n";
script_code += "chrome.runtime.sendMessage('" + chrome.i18n.getMessage("@@extension_id") + "', msg, function(response) {\n";
script_code += " console.log('response received');\n";
script_code += "});\n";
Run Code Online (Sandbox Code Playgroud)
然后将其注入网页,当它被执行时,我得到:
未捕获的TypeError:无法调用未定义的方法'sendMessage'
谁能帮我解决这个问题?
谢谢
我有一个网页,里面有我自己需要执行的脚本和变量,并从我的扩展的Background.js中检索返回值.
我理解(我认为!)为了与网页进行交互,必须通过chrome.tabs.executeScript或ContentScript来完成,但因为代码必须在原始页面的上下文中执行(为了具有范围)对于脚本和变量),需要先将其注入页面.
在Rob W的这篇精彩文章之后,我能够调用页面级脚本/变量,但我很难理解如何以这种方式返回值.
这是我到目前为止所得到的......
网页代码(我想与之交互):
<html>
<head>
<script>
var favColor = "Blue";
function getURL() {
return window.location.href;
}
</script>
</head>
<body>
<p>Example web page with script content I want interact with...</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
manifest.json:
{
// Extension ID: behakphdmjpjhhbilolgcfgpnpcoamaa
"name": "MyExtension",
"version": "1.0",
"manifest_version": 2,
"description": "My Desc Here",
"background": {
"scripts": ["background.js"]
},
"icons": {
"128": "icon-128px.png"
},
"permissions": [
"background",
"tabs",
"http://*/",
"https://*/",
"file://*/", //### (DEBUG ONLY)
"nativeMessaging"
]
}
Run Code Online (Sandbox Code Playgroud)
background.js …
我试图根据xhr调用的输出更改页面的内容.我从content.js发送一条消息,在后台js文件中进行xrh调用,然后将输出传递给content.js,这会改变页面的内容.
从我的content.js文件中我正在做以下事情.
var s = document.createElement('script');
s.src = chrome.extension.getURL('src/content/main.js');
(document.head || document.documentElement).appendChild(s);
Run Code Online (Sandbox Code Playgroud)
在我的main.js我做
chrome.runtime.sendMessage({
method: 'GET',
action: 'xhttp',
url: myurl
}, function(responseText) {
console.log("Response Text is ", responseText);
});
Run Code Online (Sandbox Code Playgroud)
在我,bg.js我有以下
chrome.runtime.onMessage.addListener(function(request, sender, callback) {
if (request.action == "xhttp") {
var xhttp = new XMLHttpRequest();
var method = request.method ? request.method.toUpperCase() : 'GET';
xhttp.onload = function() {
callback(xhttp.responseText);
};
xhttp.onerror = function() {
// Do whatever you want on error. Don't forget to invoke the …Run Code Online (Sandbox Code Playgroud) 我正在构建一个 Chrome 扩展,它使用针对特定 Web 域定制的 JavaScript 库。这是对该库的限制,只能从该特定域调用(实际上是许可证问题)。
我想将该 js 库加载到我的 Chrome 扩展中,但该扩展给出了内容安全策略错误,因为要使用的库来自 HTTP(不是 HTTPS)URL。我在 StackOverflow 中搜索了此内容,我看到的唯一选项是本地加载该文件或通过消息传递加载该文件。
有人可以告诉我在 my 中应该有什么代码background.js以及content.js在 my 中添加什么权限吗manifest.json?
获取该 js 库的网站是,例如http://library.com/js/xy.js。我知道应该还有其他东西,比如 content.js 中的一些代码。如何从该域加载代码?
清单.json:
{
"manifest_version": 2,
"name": "abc",
"version": "0.2",
"description": "abc",
"content_security_policy": "script-src 'self'; object-src 'self'",
"browser_action": {
"default_icon": "MM_logo_2009.png",
"default_popup": "tab/popup.html"
},
"background": {
"scripts": ["event.js"],
"persistent": false
},
"permissions": [
"http://library.com/js/xy.js",
"bookmarks",
"tabs",
"storage",
"http://*/*",
"https://*/*"
]
}
Run Code Online (Sandbox Code Playgroud)
事件.js:
var xhr = new XMLHttpRequest();
xhr.open("GET", …Run Code Online (Sandbox Code Playgroud) html javascript xmlhttprequest message-passing google-chrome-extension
我正在开发一个 Chrome 扩展程序,我想在加载所有页面脚本后加载它。
网站页面包含这样的代码:
<html>
<body>
<div id="main">All body elements, texts, etc...</div>
<script>
window.netflix = window.netflix || {};
netflix.falkorCache = {"genres":"58"}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我的扩展代码是:
$(document).ready(function() {
launchPlugin();
function launchPlugin() {
console.log(typeof(window.netflix));//I have also tried just `netflix`
if(typeof(window.netflix)!=='undefined'){
startNetflixPlayer();
} else{
setTimeout(
function(){
launchPlugin();
}, 700);
}
}
//other code...
});
Run Code Online (Sandbox Code Playgroud)
但插件永远不会运行,startNetflixPlayer()因为window.netflix总是undefined。我也尝试过检查变量netflix,但它也返回undefined。
同时,如果我尝试检查console.log(window.netflix)Chrome 的开发人员控制台,我会看到结果。
console.log(typeof(window.netflix))返回object。但是在这个 Chrome 扩展之后仍然看不到这个变量并返回undefined.
我相信我错过了一些简单的东西,但无法理解到底是什么。
有什么想法如何使用扩展程序的脚本从网站访问变量(和对象数据)?
PS我也检查了这个答案,但它对我不起作用: chrome扩展:在页面完成加载javascript后运行脚本
我正在编写一个 Firefox 扩展,需要监听浏览器和服务器之间的通信。对于 HTTP 通信,我在后台脚本中使用 webRequest 库。但根据这个 Mozilla 错误报告,我无法使用 webRequest 拦截 WebSocket 消息。我的扩展如何监听 WebSocket 通信?
更新:
我已经注入了我的WebSocket 包装器,但它从未被调用!我试图监听的 WebSocket 位于 iframe 内。这有关系吗?
javascript intercept websocket google-chrome-extension firefox-addon-webextensions
我想删除一个包含onsubmit内联定义的表单的事件处理程序.
<form id="loginForm" onsubmit="return performTask(this);">
Run Code Online (Sandbox Code Playgroud)
我已经尝试过:
$("#loginForm").off("submit")
$("#loginForm").unbind("submit")
$("#loginForm").die("submit")
$("#loginForm").removeAttr("onsubmit")
$("#loginForm").removeProp("onsubmit")
$("#loginForm").attr("onsubmit", "")
$("#loginForm").prop("onsubmit, "")
$("#loginForm")[0].onsubmit = null
$("#loginForm")[0].onsubmit = undefined
$("#loginForm")[0].onSubmit = null
$("#loginForm")[0].onSubmit = undefined
Run Code Online (Sandbox Code Playgroud)
什么都行不通!
我在j()的jquery方法中添加了我自己的事件监听器,但它永远不会被调用.它是旧的事件监听器在我之前执行的...它与按钮上的onClick事件做同样的事情.
我必须明确表示我正在使用chrome扩展,更确切地说是在页面中注入的内容脚本中.
所以问题是,有没有办法清除事件处理程序?或者更好的是有没有办法添加一个将在内联处理程序之前调用的事件监听器?
编辑:经过许多丑陋的代码,我找到了一种方法来做我想要的...我做了一个表单的副本,删除内联envent处理程序,在我的dom旧表单中替换然后创建我的事件处理程序.这很难看,但它有效...如果有人能解释为什么我不能这样做...
javascript jquery events google-chrome google-chrome-extension
javascript ×10
jquery ×3
firefox-addon-webextensions ×2
events ×1
firefox ×1
html ×1
intercept ×1
websocket ×1