我正在尝试使用用户脚本更改页面中的变量.我知道在源代码中有一个变量
var smilies = false;
Run Code Online (Sandbox Code Playgroud)
从理论上讲,我应该能够改变它:
unsafeWindow.smilies = true;
Run Code Online (Sandbox Code Playgroud)
但它不起作用.当我试图警告或将变量记录到控制台而没有劫持时,我得到它是未定义的.
alert(unsafeWindow.smilies); // undefined !!!
Run Code Online (Sandbox Code Playgroud)
编辑:如果它改变了什么,我正在使用Chrome ...
http://code.google.com/chrome/extensions/content_scripts.html说:
内容脚本在称为孤立世界的特殊环境中执行.他们可以访问注入页面的DOM,但不能访问页面创建的任何JavaScript变量或函数.它将每个内容脚本视为在其运行的页面上没有执行其他JavaScript.
这是关于Chrome扩展,但我猜这与用户脚本也是一样的?
谢谢你,Rob W.所以需要它的人的工作代码:
var scriptText = "smilies = true;";
var rwscript = document.createElement("script");
rwscript.type = "text/javascript";
rwscript.textContent = scriptText;
document.documentElement.appendChild(rwscript);
rwscript.parentNode.removeChild(rwscript);
Run Code Online (Sandbox Code Playgroud) 我正在寻找网页内的功能te激活chrome扩展.
想象一下,http://www.example.com/test.html包含:
<script>
hello();
</script>
Run Code Online (Sandbox Code Playgroud)
我的背景页面包含hello函数的定义:
function hello() {
alert("test");
}
Run Code Online (Sandbox Code Playgroud)
如何确保hello在test.html通话时调用Chrome扩展程序的后台页面hello();?
javascript google-chrome google-chrome-extension content-script
$x("//a[contains(@href,'.jpg')]");
Run Code Online (Sandbox Code Playgroud)
从开发人员工具命令提示符按预期工作.但是,当在扩展程序的内容脚本中,我得到一个' $x is not defined'.
为什么这在内容脚本中不可用,或者是否有特殊的方式在内容脚本/ Chrome扩展程序中访问它?
我在Debian上使用Chrome 22.
javascript xpath google-chrome google-chrome-extension content-script
我尝试创建一个 shadow DOM 来封装内容 Script 元素,然后应用 Bootstrap 样式,并使 Bootstrap Modal 在 shadow Dom 内工作,以便它可以从网页样式和脚本中封装自己。它可以成功显示内容,但 Bootstrap js 脚本不起作用。
我尝试在 Shadow DOM 中注入所有样式和脚本文件。Bootstrap 样式有效,Bootstrap 脚本无效
$(#id) //the usual way to access a id using jQuery
parentofShadowtree.shadowRoot.querySelector('#id') //to select a element inside shadow DOM
Run Code Online (Sandbox Code Playgroud)
我认为错误是因为 Bootstrap 脚本无法像通常那样访问元素。
我相信由于访问元素的方式发生了变化,这些脚本文件不起作用。
我对吗?有没有办法克服这个问题
我的比赛计划:
"content_scripts" : [
{
"matches" : [
"https://stackoverflow.com/questions#epic*"
],
"js" : ["silly.js"]
}
],
Run Code Online (Sandbox Code Playgroud)
因此,如果用户访问网页(如https://stackoverflow.com/questions),然后添加#epic它将转到https://stackoverflow.com/questions#epic但是会#epic在URL的末尾,这将激活内容脚本silly.js.
这应该发生的事情,但这不起作用.
javascript manifest google-chrome-extension hashtag content-script
对于Google-Chrome扩展程序,我想在所有Google网页上加载内容脚本.做这个的最好方式是什么?
我试过这个,manifest.json但它不起作用:
"matches": ["http://www.google.*/*", "https://www.google.*/*"],
Run Code Online (Sandbox Code Playgroud)
这"有效"但写起来有点长,我不认为这是最好的做法:
"matches": ["http://www.google.com/*", "https://www.google.com/*", "http://www.google.fr/*", "https://www.google.fr/*", "http://www.google.de/*", "https://www.google.de/*", etc..."],
Run Code Online (Sandbox Code Playgroud) google-chrome manifest internationalization google-chrome-extension content-script
Chrome扩展程序:我正在寻找一种方法来分配全局键盘快捷键,即使当前选项卡没有内容(因此也没有内容脚本),也可以调用这些快捷键.此类标签的一些示例:"新标签"标签,chrome://扩展标签,"未加载页面"标签等用于此类要求的用例包括关闭标签,转到下一个/上一页标签等操作.
该chrome.commands API允许一个做到这一点.但是,似乎没有办法让用户配置这些键盘快捷键,这是我真正希望我的扩展允许的.
有没有办法获得不需要内容脚本的可配置键盘快捷键?
global keyboard-shortcuts google-chrome-extension content-script
我正在开发一个扩展,当用户在弹出窗口上按下按钮时,我必须从linkedin配置文件页面中提取数据.我,正在将消息从popup.js页面传递给contentscript,作为回应,我将通过内容脚本从linkedin配置文件页面中提取数据,以便我可以在popup.html中显示它.但是,当我检查popup.html时,我收到了错误.错误是:
Port: Could not establish connection. Receiving end does not exist. lastError:29
Error in event handler for 'undefined': Cannot read property 'farewell' of undefined
TypeError: Cannot read property 'farewell' of undefined
at chrome-extension://kdfgoafjicddfffdbfofdmckejemfije/popup.js:6:25
at <error: illegal access>
at Event.dispatchToListener (event_bindings:356:21)
at Event.dispatch_ (event_bindings:342:27)
at Event.dispatch (event_bindings:362:17)
at Object.chromeHidden.Port.dispatchOnDisconnect (miscellaneous_bindings:258:27)
Run Code Online (Sandbox Code Playgroud)
作为参考,我的清单文件是:
{
"name": "SoftwareGrid",
"version": "0.5",
"icons": { "16": "icons/16.png","48": "icons/48.png", "128": "icons/128.png" },
"description": "Shows user cresidentials on Linkedin",
"permissions": [
"cookies",
"tabs",
"http://www.linkedin.com/*"
],
"browser_action": {
"default_title": "Show Profile", …Run Code Online (Sandbox Code Playgroud) javascript message-passing google-chrome-extension content-script
每当我尝试使用该chrome.cookies.get()函数从cookie中读取时,我都会收到此错误:
TypeError: Cannot read property 'get' of undefined.
Run Code Online (Sandbox Code Playgroud)
我在我的content.js文件中调用该函数,它只能在twitter.com上运行(该部分有效).
这是我的清单文件:
{
"manifest_version": 2,
"name": "Twitter-MultiSignin",
"description": "twiter sign in",
"version": "1.0",
"permissions": [ "cookies", "alarms" , "http://*/*", "https://*/*", "storage"],
"content_scripts": [{
"matches": ["http://twitter.com/*","https://twitter.com/*"],
"js": ["jquery.js","content.js"]
}],
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的content.js(它总是在twitter页面上提醒,以便工作正常):
$(function() {
alert('got here');
try{
chrome.cookies.get({ url: 'https://twitter.com', name: 'auth_token' }, function (cookie){
alert(cookie.value);
});
}catch(err){
//do nothing
alert(err);
}
try{
chrome.cookies.get({ url: 'https://twitter.com', name: 'twid' },
function (cookie) {
if (cookie) { …Run Code Online (Sandbox Code Playgroud) 我在 Chrome 扩展程序中有一个用于传递消息的内容脚本。每隔一段时间,当内容脚本调用时
chrome.runtime.sendMessage({
message: 'hello',
});
Run Code Online (Sandbox Code Playgroud)
它抛出一个错误:
Uncaught Error: Extension context invalidated.
Run Code Online (Sandbox Code Playgroud)
这个错误是什么意思?我找不到任何有关它的文档。
它不会持续发生。事实上,它很难重现。如果我只是让页面在后台打开一段时间,似乎就会发生这种情况。
另一个线索:我已经编写了许多带有传递消息的内容脚本的 Chrome 扩展,但我以前没有见过这个错误。主要区别在于该内容脚本是由后台页面使用注入的
chrome.tabs.executeScript({
file: 'contentScript.js',
});
Run Code Online (Sandbox Code Playgroud)
使用executeScript而不是清单文件是否会以某种方式改变内容脚本的生命周期?
content-script ×10
javascript ×5
manifest ×2
global ×1
hashtag ×1
shadow-dom ×1
userscripts ×1
xpath ×1