问题描述
我正在为Google Chrome编写用户脚本,自动打开特定的聊天选项卡,但不起作用。
我认为这是因为 Chat.openTab
没有特别定义,因为当我在javascript控制台中运行代码工作正常。
代码: / p>
var face =facebook.com
var domain = document.domain
if(domain = face )
{
Chat.openTab(sam.sebastian1,Seb)
}
其他答案指出应该是(domain == face)
strong>是一个错误。
但是,这不是阻止脚本按照您预期的方式工作。 / p>
主要问题是Chrome用户脚本无法使用目标页面中定义的JS。您必须将代码注入页面,如下所示:
function functionToInject(){
function myCode(){
/ * ---这是你把你想要做的一切的地方,
要求使用页面的javascript。
* /
var face =facebook.com
var domain = document.domain
if(domain == face)
{
聊天。 openTab(sam.sebastian1,Seb);
}
}
myCode();
}
函数addJS_Node(text,s_URL){
var scriptNode = document.createElement('script');
scriptNode.type =text / javascript;
if(text)scriptNode.textContent = text;
if(s_URL)scriptNode.src = s_URL;
var targ = document.getElementsByTagName('head')[0]
|| document.body || document.documentElement中;
targ.appendChild(scriptNode);
}
addJS_Node('('+ functionToInject.toString()+')()');
然而,因为这是Facebook,事情有点复杂。
- Facebook加载许多iFrames,
-
聊天
对象不会立即加载。
为了解决这些障碍,我们设置了一个定时器,它不会在找到资源之前尝试执行代码。
像这样:
function functionToInject(){
function myCode(){
/ * ---这是你想要做的所有事情,
要求使用页面的javascript。
* /
var face =facebook.com
var domain = document.domain
if(domain == face)
{
聊天。 openTab(sam.sebastian1,Seb);
}
}
var waitForKeyElements = setInterval(checkForElement,500);
函数checkForElement(){
if(typeof Chat!=undefined){
clearInterval(waitForKeyElements);
myCode();
}
}
}
函数addJS_Node(text,s_URL){
var scriptNode = document.createElement('script');
scriptNode.type =text / javascript;
if(text)scriptNode.textContent = text;
if(s_URL)scriptNode.src = s_URL;
var targ = document.getElementsByTagName('head')[0]
|| document.body || document.documentElement中;
targ.appendChild(scriptNode);
}
addJS_Node('('+ functionToInject.toString()+')()');
I was writing user script for Google Chrome that would automatically open a specific chat tab, but it's not working,
I think that it is because the Chat.openTab
is not specifically defined, because when i run the code in the javascript console it works fine.
CODE:
var face = "facebook.com"
var domain = document.domain
if (domain = face)
{
Chat.openTab("sam.sebastian1", "Seb")
}
The other answers point out that it should be (domain == face)
, and this is an error.
However, it is not what prevented the script from appearing to work as you expected.
The main problem is that Chrome userscripts cannot use JS defined in the target page. You must inject your code into the page, like so:
function functionToInject () {
function myCode () {
/*--- This is where you put everything you want to do that
requires use of the page's javascript.
*/
var face = "facebook.com"
var domain = document.domain
if (domain == face)
{
Chat.openTab ("sam.sebastian1", "Seb");
}
}
myCode ();
}
function addJS_Node (text, s_URL) {
var scriptNode = document.createElement ('script');
scriptNode.type = "text/javascript";
if (text) scriptNode.textContent = text;
if (s_URL) scriptNode.src = s_URL;
var targ = document.getElementsByTagName('head')[0]
|| document.body || document.documentElement;
targ.appendChild (scriptNode);
}
addJS_Node ( '(' + functionToInject.toString() + ')()' );
That was the basic answer. However, since this is Facebook, things are a bit more complicated.
- Facebook loads many iFrames, and the script will trigger on many of them.
- The
Chat
object does not load right away.
To get around these obstacles, we setup a timer, that doesn't try to execute our code until the resource is found.
Like so:
function functionToInject () {
function myCode () {
/*--- This is where you put everything you want to do that
requires use of the page's javascript.
*/
var face = "facebook.com"
var domain = document.domain
if (domain == face)
{
Chat.openTab ("sam.sebastian1", "Seb");
}
}
var waitForKeyElements = setInterval (checkForElement, 500);
function checkForElement () {
if (typeof Chat != "undefined" ) {
clearInterval (waitForKeyElements);
myCode ();
}
}
}
function addJS_Node (text, s_URL) {
var scriptNode = document.createElement ('script');
scriptNode.type = "text/javascript";
if (text) scriptNode.textContent = text;
if (s_URL) scriptNode.src = s_URL;
var targ = document.getElementsByTagName('head')[0]
|| document.body || document.documentElement;
targ.appendChild (scriptNode);
}
addJS_Node ( '(' + functionToInject.toString() + ')()' );
这篇关于为什么这个用户名在Facebook上不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!