PageRenderTime 39ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/DisplayText/chrome/content/browser.js

https://github.com/NewsSleuth/NewsSleuth
JavaScript | 1371 lines | 1088 code | 134 blank | 149 comment | 175 complexity | 480fedea8ba730ba5f4d31458d2e6f52 MD5 | raw file
  1. //function TitleLocation ( ) { return "h1"; }
  2. function boxId ( ) {return "InfoBoxId";}
  3. function AuthorClass ( ) {return "InfoClass"}
  4. function TitleLocation(TitleElement)
  5. {
  6. var site = GetHost( );
  7. var file = GetLocPath ( );
  8. if ( !file.exists() )
  9. return;
  10. // Check if url is in file
  11. var fileContents = FileIO.read(file);
  12. var line = fileContents.split("\n"),
  13. len = line.length;
  14. var siteInFile = false;
  15. while(len--)
  16. {
  17. var split = line[len].split('@');
  18. if ( site === split[0] )
  19. {
  20. // Find and return the title element of page
  21. if(GetTitleElement(line[len], TitleElement))
  22. return true;
  23. siteInFile = true;
  24. }
  25. }
  26. return siteInFile;
  27. }
  28. function CheckSourceList( )
  29. {
  30. var site = GetHost( );
  31. var file = GetSourcePath( );
  32. if ( !file.exists() )
  33. return;
  34. // Check if url is in file
  35. var fileContents = FileIO.read(file);
  36. var line = fileContents.split("\n"),
  37. len = line.length-1;
  38. var siteInFile = false;
  39. while(len--)
  40. {
  41. var split = line[len].split('@');
  42. //alert(split[0] + ' : ' + split[1]);
  43. if ( site === split[0] )
  44. {
  45. //alert('returning: ' + split[1]);
  46. return split[1];
  47. }
  48. }
  49. //alert('Site is not in file');
  50. return null;
  51. }
  52. function FindAuthor()
  53. {
  54. var ae = new Object();
  55. if (AuthorLocation(ae)){
  56. var e = ae.item;
  57. //alert(e.innerHTML);
  58. // strip any <a> from line
  59. var cn = e.childNodes;
  60. var len = cn.length;
  61. //alert(cn.length);
  62. line = '';
  63. if (cn.length>0)
  64. for(var i = 0; i < len; i++)
  65. {
  66. //alert(cn[i].data + ' ' +cn[i].tagName);
  67. if (cn[i].tagName && cn[i].tagName.toLowerCase() === 'a') {
  68. //alert(cn[i].innerHTML);
  69. line += cn[i].innerHTML;
  70. }else
  71. line += cn[i].data;
  72. // alert(cn[0].data + ' ' + cn[0].innerHTML);
  73. }
  74. //alert(line);
  75. line = line.toLowerCase();
  76. var by = line.indexOf('by ');
  77. if (by == 0) {
  78. line = line.substr(3, line.length-3);
  79. }
  80. return line;
  81. }else
  82. return null;
  83. }
  84. function AuthorLocation(AuthorElement)
  85. {
  86. var site = GetHost( );
  87. var file = GetAuthLocPath ( );
  88. if ( !file.exists() )
  89. return;
  90. // Check if url is in file
  91. var fileContents = FileIO.read(file);
  92. var line = fileContents.split("\n"),
  93. len = line.length;
  94. var siteInFile = false;
  95. while(len--)
  96. {
  97. var split = line[len].split('@');
  98. if ( site === split[0] )
  99. {
  100. // Find and return the title element of page
  101. if(GetTitleElement(line[len], AuthorElement))
  102. return true;
  103. siteInFile = true;
  104. }
  105. }
  106. //alert('Site is not in file');
  107. return siteInFile;
  108. }
  109. function GetTitleElement( path, TitleElement )
  110. {
  111. var doc = content.document;
  112. var split = path.split('@'),
  113. len = split.length-1;
  114. if (len < 2) return false;
  115. var value = split[len--],
  116. type = split[len--];
  117. // get first element on path to title
  118. if (type === 'tag')
  119. cn = doc.getElementsByTagName(value)[0];
  120. else if (type === 'class')
  121. cn = doc.getElementsByClassName(value)[0];
  122. else if (type === 'id')
  123. cn = doc.getElementById(value);
  124. // If there's more to the path continue until title element is found
  125. // Happens if element directly containing title isn't unique to page
  126. while(len > 1)
  127. {
  128. value = split[len--];
  129. type = split[len--];
  130. var childNodes = cn.childNodes,
  131. size = childNodes.length;
  132. for (var i = 0; i < size; i++)
  133. {
  134. if (type === 'tag' && childNodes[i].tagName === value) {
  135. cn = childNodes[i];
  136. break;
  137. } else if (type === 'class' && childNodes[i].className === value) {
  138. cn = childNodes[i];
  139. break;
  140. } else if (type === 'id' && childNodes[i].id) {
  141. cn = childNodes[i];
  142. break;
  143. }
  144. }
  145. }
  146. // Set TitleElement to the element containing the title
  147. TitleElement.item = cn;
  148. if (cn) {
  149. return true;
  150. }
  151. return false;
  152. }
  153. var popup = false;
  154. //These variables allow us to pass the wikipedia results to callback functions.
  155. //var authorData = new String("");
  156. //var publisherData = new String("");
  157. //These variables allow the callback functions to know the state of the program,
  158. //since we can't pass arguments in. Option lets callWikipediaAPI know how it
  159. //should call controversiesP. doAuthor lets successDump konw whether it should call
  160. //controversiesP.
  161. var numLookups = 0;
  162. var paragraphCount = 0;
  163. var infoArray;
  164. var isAuthArray;
  165. var DisplayText = {
  166. onCommand: function(event) {
  167. var doc = content.document;
  168. var ae = new Object();
  169. if (AuthorLocation(ae)){
  170. var e = ae.item;
  171. alert(e.innerHTML);
  172. var line = e.innerHTML.toLowerCase();
  173. var by = line.indexOf('by ');
  174. if (by == 0) {
  175. line = line.substr(3, line.length-3);
  176. }
  177. alert(line);
  178. }else
  179. alert('author not found');
  180. return;
  181. var p = doc.getElementById('pSummary_id');
  182. var a = doc.getElementById('aSummary_id');
  183. alert(p.innerHTML + ' ' + a.innerHTML);
  184. //changeInfoLocation();
  185. return;
  186. var div = content.document.getElementById('lookup_id');
  187. var cn = div.childNodes;
  188. var len = cn.length;
  189. while(len--)
  190. {
  191. div.removeChild(cn[len]);
  192. }
  193. return;
  194. createTopBar( );
  195. var doc = content.document;
  196. var bar = doc.getElementById('bar_id');
  197. var box = doc.createElement('div');
  198. box.id = 'bar_info_id';
  199. var top = doc.body;
  200. bar.appendChild(box);var ext = content.document.createElement("script");
  201. ext.type = "text/javascript";
  202. ext.src = "chrome://DisplayText/content/slideElement.js";
  203. top.appendChild(ext);
  204. return;
  205. }
  206. };
  207. function changeInfoLocation ( )
  208. {
  209. // remove box from page
  210. var div = content.document.getElementById(boxId());
  211. if (div)
  212. {
  213. div.parentNode.removeChild(div);
  214. }
  215. // createTopBar with different text
  216. text = 'Click here to change NewsSleuth infobox location';
  217. createTopBar('', text);
  218. }
  219. function createTopBar (text1, text2)
  220. {
  221. // creates bar at top of screen if extension can't find the title location on the page
  222. var doc = content.document,
  223. top = doc.body,
  224. bar = doc.createElement('div'),
  225. text = doc.createTextNode(text1);//'NewsSleuth was unable to find the title for this page. ');
  226. bar.className = 'topBarClass';
  227. bar.id = 'bar_id';
  228. bar.appendChild(text);
  229. top.insertBefore(bar, top.firstChild);
  230. var input = doc.createElement('a'),
  231. atext = doc.createTextNode(text2);//'Select title to view information');
  232. input.id = 'bar_a_id';
  233. input.appendChild(atext);
  234. input.addEventListener('click', selectTitle, true);
  235. bar.appendChild(input);
  236. }
  237. function selectTitle()
  238. {
  239. var div = content.document.getElementById('bar_id'),
  240. cn = div.childNodes,
  241. len = cn.length;
  242. while(len--) {
  243. div.removeChild(cn[len]);
  244. }
  245. var text = content.document.createTextNode
  246. ("Click an element on the page to place the infobox there");
  247. div.appendChild(text);
  248. content.document.addEventListener("mousedown", mouseHandler, true);
  249. }
  250. var lastElement, lastClass;
  251. function mouseHandler(event)
  252. {
  253. // displays box around element clicked on by user when trying
  254. // to select where they title is on page.
  255. if (lastElement && content.document.getElementById('titleSearchId'))
  256. {
  257. var accept = content.document.getElementById('titleSearchId');
  258. accept.parentNode.insertBefore(lastElement, accept);
  259. accept.parentNode.removeChild(accept);
  260. }
  261. if (!event) event = window.event;
  262. var element = (event.target || event.srcElement);
  263. // check if they clicked the accept button
  264. if (element.id === 'titleSearchButton') {
  265. setUpTitleLocation(lastElement);
  266. return;
  267. }
  268. lastElement = element;//event.target || event.srcElement;
  269. var pe = lastElement.parentNode,
  270. ne = lastElement.nextSibling;
  271. var div = content.document.createElement('div');
  272. div.className = 'titleSelect';
  273. div.id = 'titleSearchId';
  274. pe.insertBefore(div, lastElement);
  275. pe.removeChild(lastElement);
  276. div.appendChild(lastElement);
  277. var button = content.document.createElement('input');
  278. button.type = 'button';
  279. button.value = 'accept';//lastElement.tagName;
  280. button.id = 'titleSearchButton';
  281. //button.addEventListener('click', setUpTitleLocation, true);
  282. var par = content.document.createElement('p');
  283. par.className = 'titleSearchPar';
  284. par.appendChild(button);
  285. div.appendChild(par);
  286. }
  287. function setUpTitleLocation( element )
  288. {
  289. var doc = content.document,
  290. cn = element,
  291. path = '',
  292. count = 4,
  293. unique = false;
  294. // Checks for an element that has a unique tag, class, or id that can be
  295. // used to find the title - checks at most 3 elements from title
  296. while (count--)
  297. {
  298. // check if title has a unique class name
  299. if (cn.className && doc.getElementsByClassName(cn.className).length === 1)
  300. {
  301. //alert('unique class: ' + cn.className);
  302. path += '@class@' + cn.className;
  303. unique = true;
  304. break;
  305. }
  306. else if (cn.id)
  307. {
  308. //alert('unique id: ' + cn.id);
  309. path += '@id@' + cn.id;
  310. unique = true;
  311. break;
  312. }
  313. else if (cn.tagName && doc.getElementsByTagName(cn.tagName).length === 1)
  314. {
  315. //alert('unique tag: ' + cn.tagName);
  316. path += '@tag@' + cn.tagName;
  317. unique = true;
  318. break;
  319. }
  320. // If cn doesn't have a unique identifier than move to element containing cn
  321. // also have to check if there is a way to find cn within it's parent element
  322. // ie. check if cn has a unique class, tag, or id within the parent element
  323. var parent = cn.parentNode,
  324. childNodes = parent.childNodes,
  325. length = childNodes.length,
  326. tagCount = 0,
  327. classCount = 0,
  328. cLoc = -1;
  329. while(length--)
  330. {
  331. if (childNodes[length].tagName && childNodes[length].tagName === cn.tagName) {
  332. tagCount++;
  333. }
  334. if (childNodes[length].className && childNodes[length].className === cn.className) {
  335. classCount++;
  336. }
  337. if (childNodes[length] === cn) {
  338. cLoc = length;
  339. for (var i = 0; i < length; i++) {
  340. if (!childNodes[i].tagName)
  341. cLoc--;
  342. }
  343. }
  344. }
  345. if (tagCount === 1)
  346. path += '@tag@' + cn.tagName;
  347. else if (classCount === 1)
  348. path += '@class@' + cn.className;
  349. else if (cn.id)
  350. path += '@id@' + cn.id;
  351. else if (cLoc === 0) {
  352. path += '@tag@' + cn.tagName;
  353. }
  354. else
  355. break;
  356. cn = cn.parentNode;
  357. }
  358. if (unique)
  359. {
  360. var file = GetLocPath( );
  361. if ( !file.exists() ) {
  362. FileIO.create(file);
  363. }
  364. var site = GetHost( );
  365. var entry = site + path;
  366. FileIO.write(file, entry + '\n', 'a');
  367. }
  368. else
  369. {
  370. alert('The element you selected is not unique. '+
  371. 'NewsSleuth will display the infobox there for now, '+
  372. +'but will not remember the location.');
  373. var pn = element.parentNode;
  374. var div = content.document.createElement('div');
  375. div.id = boxId();
  376. pn.insertBefore(div, element.nextSibling);
  377. }
  378. doc.removeEventListener("mousedown", mouseHandler, true);
  379. var bar_div = content.document.getElementById('bar_id');
  380. bar_div.parentNode.removeChild(bar_div);
  381. EditPage(true);
  382. }
  383. function AddPageStyle ( )
  384. {
  385. // Add style to page
  386. var HeadOfPage = content.document.getElementsByTagName("Head")[0];
  387. var style = content.document.createElement("link");
  388. style.id = "headertext-style";
  389. style.type = "text/css";
  390. style.rel = "stylesheet";
  391. style.href = "chrome://DisplayText/content/header-text.css";
  392. HeadOfPage.appendChild(style);
  393. }
  394. function AuthorFound ( )
  395. {
  396. dump("AuthorFound()\n");
  397. // Code runs when its triggered by the extraction code
  398. // or if triggered by slideElement.js
  399. var doc = content.document;
  400. var AuthorElement = doc.getElementById('HiddenAuthor');
  401. var author = AuthorElement.value;
  402. var publication = doc.getElementById('HiddenPublication').value;
  403. if (publication === 'none')
  404. publication = null;
  405. var pub;
  406. if ((pub = CheckSourceList( )))
  407. {//alert('load publication');
  408. publication = pub;
  409. doc.getElementById('HiddenPublication').value = pub;
  410. }
  411. //alert("'"+author+"'" + ' ' + "'"+publication+"'");
  412. if (!author || author === 'none' || author == "RSS error")
  413. {
  414. if (!(author = FindAuthor()))
  415. {
  416. if (publication && publication != 'none'){
  417. callWikipediaAPI(null, publication);
  418. } else {
  419. AuthorNotFound(doc.getElementById('publication_id'), false, 'Publication name not found. Input here:');
  420. }
  421. AuthorNotFound(doc.getElementById('info_id'), true, 'Author name not found. Input here:');
  422. return;
  423. }
  424. else
  425. {
  426. // set hiddenauthor value
  427. AuthorElement.value = author;
  428. }
  429. }
  430. if (!publication)
  431. AuthorNotFound(doc.getElementById('publication_id'), false, 'Publication name not found. Input here:');
  432. author = fixAuthor(author);
  433. callWikipediaAPI(author, publication);
  434. }
  435. function AuthorNotFound (lookupDiv, isAuth, text)
  436. {
  437. var doc = content.document;
  438. // check if both not found
  439. var slide = false;
  440. if(isAuth) {
  441. if (doc.getElementById('sourceInputId'))
  442. slide = true;
  443. } else {
  444. if (doc.getElementById('authorInputId'))
  445. slide = true;
  446. }
  447. if (slide)
  448. {
  449. div = doc.getElementById('content_id');
  450. doc.getElementById('toggle_id').click();
  451. div.hidden = false;
  452. div.value = false;
  453. }
  454. // var lookupDiv = doc.getElementById('lookup_id');
  455. // var lookupDiv = doc.getElementById('info_id');
  456. var div = doc.createElement('div');
  457. div.id = 'lookupLabelId';
  458. var text = doc.createTextNode(text);//"Failed to access RSS feed. Enter author name for info");
  459. div.appendChild(text);
  460. lookupDiv.appendChild(div);
  461. div = doc.createElement('div');
  462. var input = doc.createElement('input');
  463. input.label = 'Author';
  464. if (isAuth)
  465. input.id = 'authorInputId';
  466. else
  467. input.id = 'sourceInputId';
  468. div.appendChild(input);
  469. lookupDiv.appendChild(div);
  470. div = doc.createElement('div');
  471. var button = doc.createElement('input');
  472. button.type = 'button';
  473. button.value = 'look up';
  474. var b2;
  475. if (isAuth)
  476. button.id = 'lookupButtonId';
  477. else
  478. {
  479. button.id = 'sourceButtonId';
  480. // add 'lookup and save' button
  481. b2 = doc.createElement('input');
  482. b2.type = 'button';
  483. b2.id = 'sourceButtonId';
  484. b2.value = 'lookup & save for this site';
  485. }
  486. div.appendChild(button);
  487. if (!isAuth) div.appendChild(b2);
  488. lookupDiv.appendChild(div);
  489. if (isAuth) {
  490. button.addEventListener('click', lookUpAuthor, true);
  491. input.addEventListener('keypress', function(event) { checkAReturn(event); }, false);
  492. }else{
  493. button.addEventListener('click', lookUpSource, true);
  494. input.addEventListener('keypress', function(event) { checkSReturn(event); }, false);
  495. b2.addEventListener('click', lookupSaveSource, true);
  496. }
  497. }
  498. function checkAReturn (e){
  499. // check if the user hit the return key
  500. if (e.keyCode === 13) {
  501. content.document.getElementById('lookupButtonId').click();
  502. }
  503. }
  504. function checkSReturn (e){
  505. // check if the user hit the return key
  506. if (e.keyCode === 13) {
  507. content.document.getElementById('sourceButtonId').click();
  508. }
  509. }
  510. function lookUpAuthor ()
  511. {
  512. dump("lookUpAuthor()\n");
  513. var doc = content.document;
  514. var author = doc.getElementById('authorInputId').value;
  515. if (author === '')
  516. return;
  517. var hidden = doc.getElementById('HiddenAuthor');
  518. hidden.value = author;
  519. var aslide = doc.getElementById('a_slide_id');
  520. aslide.click();
  521. aslide.value = 'slide';
  522. // Delete author lookup elements
  523. var div = doc.getElementById('info_id'),
  524. cn = div.childNodes,
  525. len = cn.length;
  526. while (len--)
  527. {
  528. div.removeChild(cn[len]);
  529. }
  530. findAuthor(author);
  531. callWikipediaAPI(fixAuthor(author), null);
  532. }
  533. function lookupSaveSource()
  534. {
  535. //alert('save source');
  536. var doc = content.document;
  537. var source = doc.getElementById('sourceInputId').value;
  538. if (source === ''){
  539. //alert('null');
  540. return;
  541. }
  542. var hidden = doc.getElementById('HiddenPublication');
  543. hidden.value = source;
  544. var pslide = doc.getElementById('p_slide_id');
  545. pslide.click();
  546. pslide.value = 'slide';
  547. // Delete source lookup elements
  548. var div = doc.getElementById('publication_id'),
  549. cn = div.childNodes,
  550. len = cn.length;
  551. while (len--)
  552. {
  553. div.removeChild(cn[len]);
  554. }
  555. //source = fixAuthor(source);
  556. // write to file
  557. var file = GetSourcePath( );
  558. if ( !file.exists() ) {
  559. FileIO.create(file);
  560. }
  561. var site = GetHost( );
  562. var entry = site + '@' + source;
  563. //alert(entry);
  564. FileIO.write(file, entry + '\n', 'a');
  565. callWikipediaAPI(null, source);
  566. }
  567. function lookUpSource ()
  568. {
  569. dump("lookUpAuthor()\n");
  570. var doc = content.document;
  571. var source = doc.getElementById('sourceInputId').value;
  572. if (source === ''){
  573. //alert('null');
  574. return;
  575. }
  576. var hidden = doc.getElementById('HiddenPublication');
  577. hidden.value = source;
  578. var pslide = doc.getElementById('p_slide_id');
  579. pslide.click();
  580. pslide.value = 'slide';
  581. // Delete source lookup elements
  582. var div = doc.getElementById('publication_id'),
  583. cn = div.childNodes,
  584. len = cn.length;
  585. while (len--)
  586. {
  587. div.removeChild(cn[len]);
  588. }
  589. //source = fixAuthor(source);
  590. //findAuthor(source);
  591. callWikipediaAPI(null, source);
  592. }
  593. function findAuthor(searchText, searchNode) {
  594. var regex = new RegExp(searchText, 'i'),
  595. childNodes = (searchNode || content.document.body).childNodes,
  596. cnLength = childNodes.length,
  597. excludes = 'html,head,style,title,link,script,object,iframe';
  598. while (cnLength--) {
  599. var currentNode = childNodes[cnLength];
  600. if (currentNode.nodeType === 1 &&
  601. (excludes + ',').indexOf(currentNode.nodeName.toLowerCase() + ',') === -1) {
  602. arguments.callee(searchText, currentNode);
  603. }
  604. if (currentNode.nodeType !== 3 || !regex.test(currentNode.data) ) {
  605. continue;
  606. }
  607. //alert(currentNode.data);
  608. var split = currentNode.data.split(' ');
  609. if (split.length > 3)
  610. continue;
  611. // var split = currentNode.
  612. checkElement(currentNode);
  613. }
  614. }
  615. function checkElement(node)
  616. {
  617. var doc = content.document;
  618. var cn = node.parentNode;
  619. var path = GetHost();
  620. var count = 0;
  621. var unique = false;
  622. while (count++ < 3)
  623. {
  624. if (doc.getElementsByTagName(cn.tagName).length === 1) {
  625. //alert('unique tag ' + cn.tagName);
  626. path += '@tag@' +cn.tagName;
  627. unique = true;
  628. break;
  629. }/* else if (cn.className && doc.getElementsByClassName(cn.className).length === 1) {
  630. path += cn.className + ' ';
  631. unique = true;
  632. break;
  633. } */else if (cn.id) {
  634. path += '@id@' + cn.id;
  635. unique = true;
  636. break;
  637. }
  638. var parent = cn.parentNode,
  639. childNodes = parent.childNodes,
  640. length = childNodes.length,
  641. tagCount = 0,
  642. classCount = 0;
  643. while(length--)
  644. {
  645. if (childNodes[length].tagName && childNodes[length].tagName === cn.tagName) {
  646. tagCount++;
  647. }
  648. if (childNodes[length].className && childNodes[length].className === cn.className) {
  649. classCount++;
  650. }
  651. }
  652. if (tagCount === 1)
  653. path += '@tag@'+cn.tagName;
  654. else if (classCount === 1)
  655. path += '@class@'+cn.className;
  656. else
  657. break;
  658. // path += cn.tagName + ' ';
  659. cn = cn.parentNode;
  660. }
  661. // alert(unique + ": " + path);
  662. if (unique)
  663. {
  664. // write path to file
  665. var file = GetAuthLocPath( );
  666. if ( !file.exists() ) {
  667. FileIO.create(file);
  668. }
  669. var site = GetHost( );
  670. var entry = path;
  671. //alert(entry);
  672. FileIO.write(file, entry + '\n', 'a');
  673. }
  674. return;
  675. }
  676. function addElements(DisplayInfo)
  677. {
  678. var doc = content.document;
  679. var top = content.document.body.parentNode;
  680. var div = doc.getElementById(boxId( ));
  681. if (!div)
  682. {
  683. div = doc.createElement('div');
  684. div.id = boxId( );
  685. div.className = AuthorClass( );
  686. // retrieve location of title element and add author box after it
  687. var TitleElement,
  688. cn = new Object();
  689. if (!TitleLocation (cn))
  690. return;
  691. TitleElement = cn.item;
  692. TitleElement.parentNode.insertBefore(div, TitleElement.nextSibling);
  693. }
  694. // retrieve location of title element and add author box after it
  695. // var TitleElement,
  696. // cn = new Object();
  697. // if ( TitleLocation(cn) )
  698. // {
  699. // TitleElement = cn.item;
  700. // if (!doc.getElementById(boxId()))
  701. // {
  702. // set up framework for information to be inserted into later
  703. // TitleElement.parentNode.insertBefore(div, TitleElement.nextSibling);
  704. var ContentDiv = doc.createElement('div');
  705. ContentDiv.id = 'content_id';
  706. ContentDiv.hidden = true;
  707. ContentDiv.value = DisplayInfo;
  708. var pubText = doc.createElement('div');
  709. pubText.appendChild(doc.createTextNode('Publication'));
  710. var PublicationDiv = doc.createElement('div');
  711. PublicationDiv.id = 'publication_id';
  712. var authorText = doc.createElement('div');
  713. authorText.appendChild(doc.createTextNode('Author'));
  714. var AuthorDiv = doc.createElement('div');
  715. AuthorDiv.id = 'info_id';
  716. var LookupDiv = doc.createElement('div');
  717. LookupDiv.id = 'lookup_id';
  718. LookupDiv.hidden = true;
  719. var ToggleDiv = doc.createElement('div');
  720. ToggleDiv.id = 'toggle_id';
  721. var toggleText = doc.createTextNode('Show author & source information for this article');
  722. ToggleDiv.appendChild(toggleText);
  723. var aSummary = doc.createElement('div');
  724. aSummary.id = 'aSummary_id';
  725. aSummary.hidden = true;
  726. var pSummary = doc.createElement('div');
  727. pSummary.id = 'pSummary_id';
  728. pSummary.hidden = true;
  729. var aExpand = doc.createElement('div');
  730. aExpand.id = 'aExpand_id';
  731. var pExpand = doc.createElement('div');
  732. pExpand.id = 'pExpand_id';
  733. ContentDiv.appendChild(PublicationDiv);
  734. ContentDiv.appendChild(pSummary);
  735. ContentDiv.appendChild(pExpand);
  736. ContentDiv.appendChild(AuthorDiv);
  737. ContentDiv.appendChild(aSummary);
  738. ContentDiv.appendChild(aExpand);
  739. div.appendChild(ContentDiv);
  740. div.appendChild(LookupDiv);
  741. div.appendChild(ToggleDiv);
  742. // }
  743. // }
  744. // write scripts for jquery and to handle the sliding function of div elements
  745. var jquery = content.document.createElement("script");
  746. jquery.type = "text/javascript";
  747. jquery.src = "chrome://DisplayText/content/jquery.js";
  748. var ext = content.document.createElement("script");
  749. ext.type = "text/javascript";
  750. ext.src = "chrome://DisplayText/content/slideElement.js";
  751. top.appendChild(jquery);
  752. top.appendChild(ext);
  753. }
  754. function writeScripts()
  755. {
  756. var body = content.document.body;
  757. if (body)
  758. {
  759. var ext = content.document.createElement("script");
  760. ext.type = "text/javascript";
  761. ext.src = "chrome://DisplayText/content/extraction.js";
  762. body.appendChild(ext);
  763. }
  764. else
  765. alert("no body");
  766. }
  767. function EditPage (DisplayInfo)
  768. {
  769. var TitleElement,
  770. element = new Object(),
  771. loc = false;
  772. // Retrieve the TitleElement
  773. loc = TitleLocation(element);
  774. TitleElement = element.item;
  775. // Display info if you know where the title is located or if
  776. // box div is already on page which happens if they chose a
  777. // non unique title location
  778. if (TitleElement || content.document.getElementById(boxId( )))
  779. {
  780. // Add information box framework to page
  781. addElements(DisplayInfo);
  782. //if (DisplayInfo)
  783. //{
  784. writeScripts();
  785. //}
  786. }
  787. else if (!loc)
  788. {
  789. // Write location of title if at 'h1'
  790. if (content.document.getElementsByTagName('h1').length === 1)
  791. {
  792. var site = GetHost( );
  793. var file = GetLocPath( );
  794. if ( !file.exists() ) {
  795. FileIO.create(file);
  796. }
  797. var entry = site + '@tag@h1';
  798. FileIO.write(file, entry + '\n', 'a');
  799. EditPage(DisplayInfo);
  800. }
  801. else
  802. {
  803. var text1 = 'NewsSleuth couldn\t find a header for this page.';
  804. var text2 = 'Click here to select where to place the infobox.';
  805. createTopBar (text1, text2);
  806. }
  807. }
  808. }
  809. function DisplayAuthorInfo (info, page)
  810. {
  811. //alert(page + ' ' + "'"+info+"'");
  812. if (!info || info == "") {
  813. return;
  814. }
  815. var doc = content.document;
  816. var div, nameValue;
  817. var summary = false;
  818. if (!page)
  819. {
  820. nameValue = doc.getElementById('HiddenPublication').value;
  821. nameValue = fixAuthor(nameValue);
  822. div = doc.getElementById('publication_id');
  823. if (div.childNodes.length > 0)
  824. {
  825. summary = true;
  826. div = doc.getElementById('pSummary_id');
  827. // add option to expand summary
  828. var exp = doc.getElementById('pExpand_id');
  829. addExpandOption(exp, 'p_row_id');
  830. //exp.appendChild(doc.createTextNode('expand');
  831. }
  832. }
  833. else
  834. {
  835. nameValue = fixAuthor(doc.getElementById('HiddenAuthor').value);
  836. div = doc.getElementById('info_id');
  837. if (div.childNodes.length > 0)
  838. {
  839. summary = true;
  840. div = doc.getElementById('aSummary_id');
  841. // add option to expand summary
  842. var exp = doc.getElementById('aExpand_id');
  843. addExpandOption(exp, 'a_row_id');
  844. //exp.appendChild(doc.createTextNode('Expand'));
  845. }
  846. }
  847. // Edit the 'info' to bold author name
  848. if (info === 'nopage') {
  849. var name = doc.createElement('div');
  850. name.id = 'name_id';
  851. name.appendChild(doc.createTextNode(nameValue));
  852. div.appendChild(name);
  853. // Displays 'No information found for author' message
  854. var AuthorElement = content.document.getElementById('HiddenAuthor');
  855. var author = AuthorElement.value;
  856. var contents = "No information found for ";
  857. var text = doc.createTextNode(contents);
  858. var bold = doc.createElement('b');
  859. var AuthorName = doc.createTextNode(fixAuthor(author));
  860. bold.appendChild(AuthorName);
  861. div.appendChild(text);
  862. div.appendChild(bold);
  863. } else {
  864. // Displays author's information
  865. var contents = info.toLowerCase(),
  866. author = doc.getElementById('HiddenAuthor').value.toLowerCase(),
  867. split = author.split(" "),
  868. firstname = split[0],
  869. lastname = split[split.length-1],
  870. index = 0,
  871. end;
  872. // var par = doc.createElement('p');
  873. // div.appendChild(par);
  874. if (summary)
  875. {
  876. // split the summary into new paragraphs for each bracket [ ]
  877. var offset;
  878. start = info.indexOf('[');
  879. if (start === 0)
  880. {
  881. offset = 1;
  882. while( start < info.length)
  883. {
  884. end = info.indexOf('[', start+offset);
  885. if (end === -1)
  886. end = info.length;
  887. else
  888. {
  889. // check if '[' is a split or just a bracket within text
  890. var splitBracket = false;
  891. var closeBracket = info.indexOf(']', end);
  892. if ( (closeBracket - end) < 3)
  893. splitBracket = true;
  894. if (!splitBracket)
  895. {
  896. offset = (end + 1) - start;
  897. continue;
  898. }
  899. }
  900. line = info.substr(start, end - start);
  901. var p = doc.createElement('div');
  902. p.id = 'summary_id';
  903. p.appendChild(doc.createTextNode(line));
  904. div.appendChild(p);
  905. offset = 1;
  906. start = end;
  907. }
  908. }
  909. else
  910. {
  911. // write info as 1 paragraph
  912. var p = doc.createElement('div');
  913. p.appendChild(doc.createTextNode(info));
  914. div.appendChild(p);
  915. }
  916. }
  917. else
  918. {
  919. //var n = doc.getElementById('HiddenAuthor').value;
  920. var name = doc.createElement('div');
  921. name.id = 'name_id';
  922. name.appendChild(doc.createTextNode(nameValue));
  923. var text;
  924. if (page)
  925. text = 'change author';
  926. else
  927. text = 'change source';
  928. var wrongA = doc.createElement('div');
  929. wrongA.id = 'wrongA_id';
  930. wrongA.appendChild(doc.createTextNode(text));
  931. if (page)
  932. wrongA.addEventListener('click', wrongAuthor, true);
  933. else
  934. wrongA.addEventListener('click', wrongSource, true);
  935. var nameRow = doc.createElement('tr'),
  936. c1 = doc.createElement('td'),
  937. c2 = doc.createElement('td');
  938. c1.appendChild(name);
  939. c2.appendChild(wrongA);
  940. nameRow.appendChild(c1);
  941. nameRow.appendChild(c2);
  942. var box = doc.getElementById(boxId());
  943. c1.width = box.offsetWidth - 150;
  944. c2.width = 150;
  945. // nameRow.width = div.offsetWidth;
  946. var p = doc.createElement('div');
  947. p.id = 'mainpar_id';
  948. var copy = doc.createTextNode(info);
  949. p.appendChild(copy);
  950. // div.appendChild(name);
  951. div.appendChild(nameRow);
  952. div.appendChild(p);
  953. }
  954. }
  955. // Have the information slide down rather than just appear
  956. div = doc.getElementById('content_id');
  957. if(div.value){
  958. doc.getElementById('toggle_id').click();
  959. div.hidden = false;
  960. div.value = false;
  961. } else if (!summary){
  962. if (!page && (div = doc.getElementById('p_slide_id')).value === 'slide') {
  963. doc.getElementById('p_slide_id').click();
  964. div.value = 'clicked';
  965. } else if ((div = doc.getElementById('a_slide_id')).value === 'slide') {
  966. doc.getElementById('a_slide_id').click();
  967. div.value = 'clicked';
  968. }
  969. }
  970. }
  971. function wrongAuthor ( )
  972. {
  973. //alert('wrong author');
  974. var doc = content.document;
  975. // remove all info from author div
  976. var div = doc.getElementById('info_id'),
  977. cn = div.childNodes,
  978. len = cn.length;
  979. while(len--)
  980. {
  981. div.removeChild(cn[len]);
  982. }
  983. doc.getElementById('aExpand_id').hidden = true;
  984. // add lookup elements to box
  985. AuthorNotFound(doc.getElementById('info_id'), true, "Enter author name:");
  986. }
  987. function wrongSource ( )
  988. {
  989. //alert('wrong source');
  990. var doc = content.document;
  991. // remove all info from author div
  992. var div = doc.getElementById('publication_id'),
  993. cn = div.childNodes,
  994. len = cn.length;
  995. while(len--)
  996. {
  997. div.removeChild(cn[len]);
  998. }
  999. doc.getElementById('pExpand_id').hidden = true;
  1000. // add lookup elements to box
  1001. AuthorNotFound(doc.getElementById('publication_id'), false, "Enter name of publication:");
  1002. }
  1003. function addExpandOption (exp, id)
  1004. {
  1005. var doc = content.document;
  1006. // add option to expand summary
  1007. var row = doc.createElement('tr'),
  1008. c1 = doc.createElement('td'),
  1009. c2 = doc.createElement('td'),
  1010. c3 = doc.createElement('td');
  1011. row.id = id;//'p_row_id';
  1012. c1.id = 'a_cel_id';
  1013. c2.id = 'a_cel_id';
  1014. c3.id = 'a_cel_id';
  1015. row.appendChild(c1);
  1016. row.appendChild(c2);
  1017. row.appendChild(c3);
  1018. c1.appendChild(doc.createTextNode('v'));
  1019. c2.appendChild(doc.createTextNode('v'));
  1020. c3.appendChild(doc.createTextNode('v'));
  1021. var div = doc.getElementById(boxId());
  1022. c1.width = div.offsetWidth/3;
  1023. c2.width = div.offsetWidth/3;
  1024. c3.width = div.offsetWidth/3;
  1025. exp.appendChild(row);
  1026. }
  1027. function DisplayOnLoad ( )
  1028. // Check if preferences are set to display info when page loads
  1029. {
  1030. var prefs = Components.classes["@mozilla.org/preferences-service;1"]
  1031. .getService(Components.interfaces.nsIPrefService)
  1032. .getBranch("NewsSleuth.");
  1033. return prefs.getBoolPref("DisplayOnLoad");
  1034. }
  1035. var SetPreferences = {
  1036. onCommand: function(event) {
  1037. // Check preferences to set initial state of checkbox
  1038. var prefs = Components.classes["@mozilla.org/preferences-service;1"]
  1039. .getService(Components.interfaces.nsIPrefService)
  1040. .getBranch("NewsSleuth.");
  1041. var DisplayOnLoad = prefs.getBoolPref("DisplayOnLoad");
  1042. var arg;
  1043. if (DisplayOnLoad)
  1044. arg = true;
  1045. else
  1046. arg = false;
  1047. var strWindowFeatures = "menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes";
  1048. var win = window.openDialog(
  1049. 'chrome://DisplayText/content/menubox.xul',
  1050. 'prefWindow',
  1051. strWindowFeatures,
  1052. arg);
  1053. }
  1054. }
  1055. function CheckList ( )
  1056. // Checks user's list of sites they want author information displayed for
  1057. {
  1058. dump("inside CheckList()\n");
  1059. var URL = GetHost( );
  1060. var file = GetPath( );
  1061. if ( !file.exists() )
  1062. {
  1063. dump("file doesn't exist\n");
  1064. return;
  1065. }
  1066. dump("file exists\n");
  1067. // Check if url is in file
  1068. var fileContents = FileIO.read(file);
  1069. var line = fileContents.split("\n");
  1070. for (var i=0; i < line.length; i++)
  1071. {
  1072. if ( URL == line[i] )
  1073. {
  1074. return true;
  1075. }
  1076. }
  1077. dump("returning false\n");
  1078. return false;
  1079. }
  1080. function GetHost ( )
  1081. {
  1082. return window.content.location.hostname;
  1083. // Retrieve current URL
  1084. var url = window.content.location.href;
  1085. // alert(window.content.location.hostname);
  1086. // Shorten URL to just the host url
  1087. var start = 0;
  1088. var http = url.indexOf('http');
  1089. if (http !== -1)
  1090. start = 7;
  1091. url = url.slice(start, url.length);
  1092. var end = url.indexOf('/');
  1093. if (end == -1)
  1094. {
  1095. end = url.length;
  1096. }
  1097. var source = url.slice(0,end);
  1098. return source;
  1099. }
  1100. function GetPath ( )
  1101. {
  1102. // Returns path to profile directory
  1103. var file = DirIO.get("ProfD");
  1104. file.append("extensions");
  1105. // file.append("newssleuth@news.sleuthdir");
  1106. // if (!file.exists())
  1107. // DirIO.create(file);
  1108. file.append("SiteList.txt");
  1109. return file;
  1110. }
  1111. function GetAuthLocPath ( )
  1112. {
  1113. var file = DirIO.get("ProfD");
  1114. file.append("extensions");
  1115. // file.append("newssleuth@news.sleuth");
  1116. // if (!file.exists())
  1117. // DirIO.create(file);
  1118. file.append("AuthorLocation.txt");
  1119. return file;
  1120. }
  1121. function GetSourcePath( )
  1122. {
  1123. var file = DirIO.get("ProfD");
  1124. file.append("extensions");
  1125. // file.append("newssleuth@news.sleuth");
  1126. // if (!file.exists())
  1127. // DirIO.create(file);
  1128. file.append("SiteSource.txt");
  1129. return file;
  1130. }
  1131. function GetLocPath( )
  1132. {
  1133. var file = DirIO.get("ProfD");
  1134. file.append("extensions");
  1135. // file.append("newssleuth@news.sleuth");
  1136. // if (!file.exists())
  1137. // DirIO.create(file);
  1138. file.append("TitleLocation.txt");
  1139. return file;
  1140. }
  1141. var AddSite = {
  1142. onCommand: function(event) {
  1143. var file = GetPath ( );
  1144. if ( !file.exists() )
  1145. {
  1146. FileIO.create(file);
  1147. }
  1148. // Retrieve current URL
  1149. var url = GetHost( );
  1150. // Check if url is already on file
  1151. var fileContents = FileIO.read(file);
  1152. var line = fileContents.split("\n");
  1153. var duplicate = false;
  1154. for (var i=0; i < line.length; i++)
  1155. {
  1156. if ( url == line[i] )
  1157. {
  1158. alert ("URL already in file");
  1159. duplicate = true;
  1160. break;
  1161. }
  1162. }
  1163. if (!duplicate)
  1164. {
  1165. FileIO.write(file, url + '\n', 'a');
  1166. } else return;
  1167. // Write location of title if at 'h1'
  1168. if (content.document.getElementsByTagName('h1').length === 1)
  1169. {
  1170. var file = GetLocPath( );
  1171. if ( !file.exists() ) {
  1172. FileIO.create(file);
  1173. }
  1174. var entry = url + '@tag@h1';
  1175. FileIO.write(file, entry + '\n', 'a');
  1176. }
  1177. }
  1178. }
  1179. var RemoveSite = {
  1180. onCommand: function(event) {
  1181. var file = GetPath( );
  1182. if (!file.exists())
  1183. {
  1184. alert ("File doesn't exist");
  1185. return;
  1186. }
  1187. var URL = GetHost( );
  1188. var fileContents = FileIO.read(file);
  1189. var lines = fileContents.split("\n");
  1190. var first = true;
  1191. for (var i = 0; i < lines.length - 1; i++)
  1192. {
  1193. if ( URL != lines[i] )
  1194. {
  1195. if (first)
  1196. {
  1197. FileIO.write(file, lines[i] + '\n');
  1198. first = false;
  1199. }
  1200. else
  1201. FileIO.write(file, lines[i] + '\n', 'a');
  1202. }
  1203. else
  1204. {
  1205. }
  1206. }
  1207. }
  1208. }
  1209. var DisplaySiteList = {
  1210. onCommand: function(event) {
  1211. // Find Path to SiteList.txt
  1212. var file = GetPath( );
  1213. if (!file.exists() )
  1214. {
  1215. alert("File doesn't exist");
  1216. } else {
  1217. var fileContents = FileIO.read(file);
  1218. var line = fileContents.split("\n");
  1219. var file = GetPath( );
  1220. if (!file.exists() )
  1221. {
  1222. alert("File doesn't exist");
  1223. } else {
  1224. var fileContents = FileIO.read(file);
  1225. }
  1226. var width = 500,
  1227. height = 350,
  1228. xcor = (screen.availWidth/2) - width/2;
  1229. ycor = (screen.availHeight/2) - height/2;
  1230. // var strWindowFeatures = "width=500, height=350, chrome=yes, centerscreen=true";
  1231. var strWindowFeatures = 'width='+width+', height='+350+', top='+ycor+', left='+xcor;
  1232. var win = window.openDialog(
  1233. 'chrome://DisplayText/content/sites.xul',
  1234. 'prefWindow', strWindowFeatures , fileContents);
  1235. }
  1236. }
  1237. }