2015-10-11 21 views
0

打开我想创建一个简单的书签其中:无法在新窗口中执行功能的小书签

  1. 打开特定网站在新标签页(窗口)
  2. 并提交一个字符串,该网站的搜索领域。

我在做单独1和2的成功,但把它们在一起时 的标签,而不搜索字段接受任何输入打开。

我不知道我在做什么错在这里。 你可以建议什么解决方案?

我的代码:

(function(foo){ 

    // 1.) open new tab/window 
    var myWindow = window.open('http://www.fakespot.com/', '_blank');  
    myWindow.focus(); 

    // 2.) input + click submit button 
    document.getElementById("url").value = "test input"; 
    document.getElementsByName("commit")[0].click(); 
})('foo') 

无法正常工作或:

... 
    myWindow.document.getElementById("url").value = link; 
    myWindow.document.getElementsByName("commit")[0].click(); 
    ... 

变换JavaScript插入书签的位置: http://chriszarate.github.io/bookmarkleter/

+0

您将需要使用类似TaperMonkey – epascarello

+0

为什么Javascript让我们在这里?实际突破点在哪里? – pykong

+0

问题是您正在运行的小书签代码不适用于新窗口。使用相同的原产地政策,您需要首先登录网站,然后点击书签才能使代码正常工作。如果这是正确的,那么您需要添加代码以等待窗口加载,而不是您可以引用页面内部的表单元素。 – epascarello

回答

0

你可以完成你想什么去做 使用HTML5 localStorage。这是一个如何工作的例子。在2个独立的选项卡中打开附带的提琴并在它们之间传递消息!

// HTML(仅用于测试)

<!-- This is your parent window --> 
<input type="text" id="message1" /> 
<button type="button" value="Send Message" onclick="updateStorageEvent()">Send Message</button> 

<!-- This would be your theoretical second window --> 
<!-- Open this fiddle in a second tab then send messages to see the results in the other window--> 
<p id="messageReceived"></p> 

// JS

// Parent Window 
var data = document.getElementById('message1'); 

function updateStorageEvent() { 
    localStorage.setItem('storage-event', data.value); 
} 

// Target Window 
var output = document.getElementById('messageReceived'); 

window.addEventListener('storage', function (event) { 
    output.innerHTML = event.newValue; 
}); 

这里是链接到显示该工作小提琴。右键单击链接并选择在新选项卡中打开。这样做两次,你可以在它们之间传递消息。 http://jsfiddle.net/wpwelch/khje9Lh3/2/

+0

THX,嗡嗡声!我需要大约三天的时间来学习如何实现这一点。 – pykong

+0

我试过了,但是我看不到如何使用HTML5 localStorage绕过原始策略问题。您提出的解决方案需要手动打开目标站点,这违背了我喜欢编写的书签的目的。或者我的执行错误? – pykong