|
174 | 174 | }
|
175 | 175 |
|
176 | 176 | if (window.chrome) {
|
177 |
| - window.addEventListener('AutoPatchWork.load.from.page', load_for_chrome, false); |
178 |
| - window.addEventListener('AutoPatchWork.error.from.page', load_error_for_chrome, false); |
179 | 177 | request = request_for_chrome;
|
180 | 178 | }
|
181 | 179 | if ((next.host && next.host !== location.host) || (next.protocol && next.protocol !== location.protocol)) {
|
|
320 | 318 | window.removeEventListener('AutoPatchWork.DOMNodeInserted', restore_setup, false);
|
321 | 319 | window.removeEventListener('AutoPatchWork.state', state, false);
|
322 | 320 | window.removeEventListener('beforeunload', savePosition, false);
|
323 |
| - if (window.chrome) { |
324 |
| - window.removeEventListener('AutoPatchWork.load.from.page', load_for_chrome, false); |
325 |
| - window.removeEventListener('AutoPatchWork.error.from.page', load_error_for_chrome, false); |
326 |
| - } |
327 | 321 |
|
328 | 322 | if (status.bottom && status.bottom.parentNode) {
|
329 | 323 | status.bottom.parentNode.removeChild(status.bottom);
|
|
532 | 526 | loading = true;
|
533 | 527 | }
|
534 | 528 | var url = state.nextURL = next.href || next.getAttribute('href') || next.action || next.getAttribute('action') || next.value || next.getAttribute('value');
|
535 |
| - var s = document.createElement('script'); |
536 |
| - s.textContent = '(' + function (url) { |
537 |
| - var x = new XMLHttpRequest(); |
538 |
| - x.onload = function () { |
539 |
| - if (!x.getResponseHeader('Access-Control-Allow-Origin')) { |
540 |
| - dispatch_message_event('AutoPatchWork.load.from.page', {responseText: x.responseText, url: url}); |
541 |
| - } else { |
542 |
| - x.onerror(); |
543 |
| - } |
544 |
| - }; |
545 |
| - x.onerror = function () { |
546 |
| - dispatch_message_event('AutoPatchWork.error.from.page', {message: 'request failed. status:' + x.status}); |
547 |
| - }; |
548 |
| - x.open('GET', url, true); |
549 |
| - x.overrideMimeType('text/html; charset=' + document.characterSet); |
550 |
| - x.send(null); |
551 |
| - function dispatch_message_event(name, data, o) { |
552 |
| - o || (o = {}); |
553 |
| - var ev = document.createEvent('MessageEvent'); |
554 |
| - ev.initMessageEvent(name, o.canBubble || false, o.cancelable || false, data, o.origin || location.origin, o.id || Date.now(), o.source || window); |
555 |
| - window.dispatchEvent(ev); |
556 |
| - } |
557 |
| - } + ')("' + url + '");'; |
558 |
| - document.head.appendChild(s); |
559 |
| - document.head.removeChild(s); |
560 |
| - } |
561 | 529 |
|
562 |
| - function load_for_chrome(evt) { |
563 |
| - dispatch_event('AutoPatchWork.load', {response: {responseText: evt.data.responseText}, url: evt.data.url}); |
564 |
| - } |
565 |
| - |
566 |
| - function load_error_for_chrome(evt) { |
567 |
| - dispatch_event('AutoPatchWork.error', {message: evt.data.message}); |
| 530 | + var x = new XMLHttpRequest(); |
| 531 | + x.onload = function () { |
| 532 | + if (location.origin === (new URL(x.responseURL)).origin) { |
| 533 | + dispatch_event('AutoPatchWork.load', {response: x, url: x.responseURL}); |
| 534 | + } else { |
| 535 | + x.onerror(); |
| 536 | + } |
| 537 | + }; |
| 538 | + x.onerror = function () { |
| 539 | + dispatch_event('AutoPatchWork.error', {message: 'request failed. status:' + x.status}); |
| 540 | + }; |
| 541 | + x.open('GET', url, true); |
| 542 | + x.overrideMimeType('text/html; charset=' + document.characterSet); |
| 543 | + x.send(null); |
568 | 544 | }
|
569 | 545 |
|
570 | 546 | function request_iframe() {
|
|
676 | 652 | append_point.insertBefore(root, insert_point);
|
677 | 653 | var docHeight = documentHeight();
|
678 | 654 | var docs = get_next_elements(htmlDoc);
|
| 655 | + var elementHeight = pageElementHeight(docs[docs.length - 1]); |
679 | 656 | var first = docs[0];
|
680 | 657 | if (!first) {
|
681 | 658 | dispatch_event('AutoPatchWork.terminated', {message: 'The next page\'s pageElement was empty.'});
|
682 | 659 | htmlDoc = null;
|
683 | 660 | return;
|
684 | 661 | }
|
685 | 662 | docs.forEach(function (doc, i, docs) {
|
| 663 | + Array.prototype.forEach.call(doc.querySelectorAll('img'), function(img) { |
| 664 | + if (!img.getAttribute('src').match(/(^https?:\/\/|^data:|^\/)/)) { |
| 665 | + img.setAttribute('src', next.getAttribute('href').replace(/\/[\w:%#\$&\?\(\)~\.=\+\-]*$/, '/') + img.getAttribute('src')); |
| 666 | + } |
| 667 | + }); |
686 | 668 | var insert_node = append_point.insertBefore(document.importNode(doc, true), insert_point);
|
687 | 669 | var mutation = {
|
688 | 670 | targetNode: insert_node,
|
|
699 | 681 | docs[i] = insert_node;
|
700 | 682 | });
|
701 | 683 | if (status.bottom) status.bottom.style.height = Root.scrollHeight + 'px';
|
702 |
| - if (docHeight === documentHeight()) { |
| 684 | + if (elementHeight === pageElementHeight(docs[docs.length - 1]) && docHeight === documentHeight()) { |
703 | 685 | return dispatch_event('AutoPatchWork.error', {message: 'missing next page contents'});
|
704 | 686 | }
|
705 | 687 | next = get_next(htmlDoc);
|
|
724 | 706 | function documentHeight() {
|
725 | 707 | return Math.max(document.documentElement.scrollHeight, document.body.scrollHeight)
|
726 | 708 | }
|
| 709 | + |
| 710 | + function pageElementHeight(element) { |
| 711 | + return element.parentNode.scrollHeight; |
| 712 | + } |
727 | 713 |
|
728 | 714 | function createXHTML(str) {
|
729 | 715 | return new DOMParser().parseFromString(str, 'application/xhtml+xml');
|
|
0 commit comments