GreaseMonkey กับ ajax หา page results ใน KeywordToolExternal

เริ่มโดย raptor, 06 พฤษภาคม 2010, 19:45:35

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

raptor

ขอขอบคุณ TSB สำหรับ 18+ มากมาย ครบ 200 กระทู้แล้ว
วันนี้เสนอเรื่อง GM(GreaseMonkey) + jQuery + ajax cross domain
ตอน KeywordToolExternal กับ page results ตอนนี้ต้องใช้ php ด้วยครับ
ดู code เลยครับ ส่วนของ GM
คลิกขวาที่ icon ลิงนั้นเลยครับแล้วเลือก
> New User Script...
จะขึ้น popup ขึ้นมา
ตรงช่อง Name ใส่ว่า KeywordToolExternal (หรือชื่ออื่นก็ได้แล้วแต่)
ตรงช่อง Includes (One per line) ใส่ว่า
โค๊ด:
https://adwords.google.com/*
แล้วกด ok ใส่ code javascript ต่อ
// ==UserScript==
// @name           KeywordToolExternal
// @namespace      c:\
// @include        [url]https://adwords.google.com/[/url]*
// ==/UserScript==
// --- load jQuery ---
var include_jq = document.createElement('script');
include_jq.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js';
include_jq.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(include_jq);
function include_jq_wait() {
  if(typeof unsafeWindow.jQuery == 'undefined') { window.setTimeout(include_jq_wait, 100); }
  else { $ = unsafeWindow.jQuery; main(); }
}
include_jq_wait();

// --- main ---
function main(){
  waiting = 0;
  $("input#kpKeywordPlanner-getKeywordsButton").after('<input id="google_click" value="AddEven" type="button">');
  $("#google_click").click(function(){
     $("td.c0").click(function(){
        if(waiting==0){
           keyword = $(this).html();
//            keyword = keyword.replace(/ /g, "+");
           context = this;
           send_ajax_cross_domain(keyword);
        }
     });
     $("td.c0").css("cursor", "pointer");
     $(this).fadeOut().fadeIn();
  });
}
function ajax_cross_domain_return(output){
//   alert(output.result);
  $(context).html($(context).html() + " " + output.result);
  waiting = 0;
}
function send_ajax_cross_domain(keyword){
  waiting = 1;
  input = 'keyword=' + keyword;
  $.ajaxSetup({
     type: "GET",
     url: service_url,
     cache: false,
     async: false,
     dataType: 'jsonp',
     dataCharset: 'jsonp',
     jsonp: 'jsoncallback',
     data: input,
     success: ajax_cross_domain_return
  });
  $.ajax();
return 0;
}
var context;
var waiting;
var service_url = 'http://me.localhost/backup/service.php';

บรรทัดสุดท้ายของ code GM แก้ที่อยู่เป็น php ของท่านเองนะครับ
ส่วนนี่ code php "http://me.localhost/backup/service.php"
<?php
function get_google_result($key){
   
$url "http://www.google.com/search?hl=en&num=10&q={key}";
   
$key str_replace(" ""+"$key);
   
$url str_replace("{key}"$key$url);
   
$data file_get_contents($url);
   
$pos strpos($data"Results");
   
$result 0;
   if (!(
$pos === false)) {
      
$subdata substr($data$pos100);
      
$subdata str_replace("<b>"""$subdata);
      
$subdata str_replace("</b>"""$subdata);
      
$arrdata explode(" "$subdata);
      if (
strpos($subdata"about") === false$result $arrdata[5];
      else 
$result $arrdata[6];
//      $result = str_replace(",", "", $result);
   
}
   return 
$result;
}
function 
main(){
   
$key $_GET['keyword'];   // input key1 key2 key3
   
$output["key"] = $key;
   
$output["result"] = get_google_result($key);
   echo 
$_GET['jsoncallback'] . '('.json_encode($output).')';
   return;
}
main();
?>


วิธีใช้ ใช้กับ https://adwords.google.com/select/KeywordToolExternal อินเทอร์เฟซเก่านะครับ


click (1) ให้มันค้นหา แล้วรอให้โหลดเสร็จก่อนนะครับ
click (2) โหลดเสร็จคลิกปุ่มนี้ต่อนะครับ
หลังจากนั้นจะสามารถคลิกที่ keyword (3) ได้ครับ มันจะส่ง keyword ไปขอข้อมูลที่ php แล้วก็เอา page result กลับมาโชว์ จบ.

ในตอนนี้เป็นการนำส่งข้อมูลจากเว็บเป้าหมายมาที่ php เว็บเรา(_GET) ด้วย ajax(cross domain) แล้วให้ไปทำงานอะไรต่อก็แล้วแต่ที่ php จะทำได้เลยครับ

GM ตอนนี้คงเป็นตอนสุดท้ายครับ เนื่องจากมันเริ่มจะเลยเรื่อง GM ไปแล้ว
ถ้ามีโอกาสตอนหน้าอาจเป็นเรื่องของ iMacro ครับ

2/11/2010 ใช้ได้กับอินเทอร์เฟซเก่า แต่ตอนนี้เป็นแบบใหม่เท่านั้น เลือกแบบเก่าไม่ได้แล้ว จึงใช้ไม่ได้แล้วครับ


ปล. วิธีใช้ GM รบกวนดูจากกระทู้เก่านะครับ
http://www.thaiseoboard.com/index.php?topic=98160.0   (เก็บ url จาก google)
http://www.thaiseoboard.com/index.php?topic=98868.0   (show asin node amazon)
http://www.thaiseoboard.com/index.php?topic=104949.0   (ajax ธรรมดาใน google)

afterdead

ขายเว็บอ่านการ์ตูน Manga Traffic 4000+/days PageView 36,000/ days PM
VPS CHEAP
[direct=https://www.digitalocean.com/?refcode=66904e5523f1]สุดยอด ! CLOUD VPS SSD 20 GB RAM 512 จ่ายผ่าน Paypal หรือ บัตรเครดิต ! แค่ $5.0 คุ้มกว่านี้มีที่ไหน มีสิงค์โปร์ ใช้กับเว็บไทยสุดยอดขอบอกๆ[/direct]
Unlimited & Unlimited Hosting.
HOST GATOR COUPONS ลด 25% ใส่คูปอง "tsbgator25off" [direct=https://signup.hostgator.com/hg4/shared-29-36-tsbgator25off?]>>>Hatchling Plan<<<[/direct] |