2017-05-25 32 views
1

我是相當新的程序員,我需要一個建議我怎麼能做到這一點,瞄準輸入框在外部網頁

我的目標是能夠在例如新窗口中打開一個新的網站google.com並在搜索輸入框中輸入一個字符串,而無需用戶執行任何操作。

我看了很多想法,這將如何實施,但似乎沒有找到我可以使用的解決方案。

我做了一個按鈕,觸發此javascript函數:

function OpenPopupCenter() { 
    pageURL = 'http://www.google.com'; 
    title = 'temp title'; 
    w = (window.outerWidth * 0.67); 
    h = (window.outerHeight * 0.80); 

    var dualScreenLeft = typeof window.screenLeft !== "undefined" ? window.screenLeft : screen.left; 
    var dualScreenTop = typeof window.screenTop !== "undefined" ? window.screenTop : screen.top; 

    var width = window.innerWidth ? window.innerWidth : 
    document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width; 
    var height = window.innerHeight ? window.innerHeight : 
    document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height; 

    var left = ((width/2) - (w/2)) + dualScreenLeft -125; 
    var top = ((height/2) - (h/2)) + dualScreenTop; 
    var newWindow = window.open(pageURL, title, 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left); 

    $(newWindow).ready(function() { 
     $("#lst-ib").val("Hello World"); 
    }); 

的窗口打開顯示谷歌,但我無法目標由ID LST-IB和插入字符串「Hello World輸入框中「進入它,我做錯了,任何人都可以指導我找到正確的解決方案,或者想出一個可行的解決方案。

+0

這聽起來像是一項違反JavaScript跨域策略的任務,因此可能無法實現。請記住,一個可以控制另一個網站的網站可以訪問諸如銀行信息或電子郵件等敏感內容。也許在網站「Let Me Google That For You」中看到的「假」收購可以激發你的需求? –

回答

0

在你的情況下,這是不可能的,因爲Same-origin policy。引用的相關信息:

根據該政策,web瀏覽器允許包含在第一網頁的第二網頁訪問數據的腳本,但前提是這兩個網頁具有相同的原始

這是出於安全原因。如果沒有這項政策,任何人都可以(例如)編寫一個腳本來打開您的銀行賬戶網站,並將所有的錢轉讓給他們。

0

這裏。當谷歌想要搜索的東西,它使用基地址http://google.com/search?q=。它搜索的內容是在'='之後,用[空格]替換爲'+'。如果我想查找'遺傳算法',我會去掉空格並用'+'替換它們,所以它變成'Genetic + Algorithm'。然後我會加入這與基地網址,所以'http://google.com/search?q=Genetic+Algorithm'將是最終結果。還有其他的東西,你可以再說「搜索?Q =」,比如增加「&源= LNMS & TBM = isch &山」到最終使得做一個圖片搜索。有許多不同的標籤可以用於不同的目的。我希望這有幫助。