Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
9a96e17c
Commit
9a96e17c
authored
Jun 07, 2017
by
Juan Corona
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor FrameUtil
parent
4379b1e8
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
75 deletions
+32
-75
main.js
src/annotator/main.js
+10
-2
frame-util.js
src/annotator/util/frame-util.js
+22
-73
No files found.
src/annotator/main.js
View file @
9a96e17c
...
@@ -45,13 +45,21 @@ $.noConflict(true)(function() {
...
@@ -45,13 +45,21 @@ $.noConflict(true)(function() {
var
Klass
=
window
.
PDFViewerApplication
?
var
Klass
=
window
.
PDFViewerApplication
?
PdfSidebar
:
PdfSidebar
:
Sidebar
;
Sidebar
;
if
(
config
.
hasOwnProperty
(
'constructor'
))
{
if
(
config
.
hasOwnProperty
(
'constructor'
))
{
Klass
=
config
.
constructor
;
Klass
=
config
.
constructor
;
if
(
Klass
===
"Guest"
)
Klass
=
Guest
;
delete
config
.
constructor
;
delete
config
.
constructor
;
}
}
if
(
config
.
enableMultiFrameSupport
&&
config
.
subFrameInstance
)
{
Klass
=
Guest
;
// Other modules use this to detect if this
// frame context belongs to hypothesis.
// Needs to be a global property that's set.
window
.
__hypothesis_frame
=
true
;
}
config
.
pluginClasses
=
pluginClasses
;
config
.
pluginClasses
=
pluginClasses
;
window
.
annotator
=
new
Klass
(
document
.
body
,
config
);
window
.
annotator
=
new
Klass
(
document
.
body
,
config
);
...
...
src/annotator/util/frame-util.js
View file @
9a96e17c
'use strict'
;
'use strict'
;
// THESIS TODO: Once this module is finalized (more or less), then make
// sure to remove JQuery and refactor.
var
$
=
require
(
'jquery'
);
module
.
exports
=
{
findIFrames
,
hasHypothesis
,
injectHypothesis
,
isAccessible
,
isValid
,
};
// Find all iframes within this iframe only
// Find all iframes within this iframe only
function
findIFrames
(
iframe
)
{
function
findFrames
(
container
)
{
var
iframes
=
[];
var
frames
=
Array
.
from
(
container
.
getElementsByTagName
(
'iframe'
));
return
frames
.
filter
(
isValid
);
$
(
'iframe'
,
iframe
).
each
(
function
(
index
,
iframe
)
{
if
(
isValid
(
iframe
)
)
{
iframes
.
push
(
iframe
);
}
});
return
iframes
;
}
}
// Check if the iframe has already been injected
// Check if the iframe has already been injected
function
hasHypothesis
(
iframe
)
{
function
hasHypothesis
(
iframe
)
{
// THESIS TODO: Are there better identifiers to use?
return
iframe
.
contentWindow
.
__hypothesis_frame
===
true
;
var
scripts
=
{
src
:
[
'/hypothesis'
,
'/embed.js'
,
]
};
var
frameBody
=
iframe
.
ownerDocument
.
body
;
var
childNodes
=
frameBody
.
childNodes
;
var
hasHypothesis
=
false
;
for
(
var
i
=
0
;
i
<
childNodes
.
length
-
1
;
i
++
)
{
var
node
=
childNodes
[
i
];
if
(
node
.
tagName
!==
'SCRIPT'
)
continue
;
// Skip to next increment
for
(
var
j
=
0
;
j
<
scripts
.
src
.
length
-
1
;
j
++
)
{
var
src
=
scripts
.
src
[
j
];
if
(
node
.
src
.
includes
(
src
))
{
hasHypothesis
=
true
;
return
;
// Found our answer, stop looping.
}
}
}
return
hasHypothesis
;
}
}
// Inject embed.js into the iframe
// Inject embed.js into the iframe
function
injectHypothesis
(
iframe
,
i
)
{
function
injectHypothesis
(
iframe
,
scriptUrl
)
{
if
(
!
iframe
)
return
;
var
iframes
;
// Support arrays via recursion
if
(
iframe
.
constructor
===
Array
)
{
if
(
!
iframe
.
length
)
return
;
iframes
=
iframe
;
i
=
(
i
!=
undefined
)
?
i
:
0
;
// if set, use i. Otherwise, use 0
iframe
=
iframes
[
i
];
}
var
config
=
document
.
createElement
(
'script'
);
var
config
=
document
.
createElement
(
'script'
);
config
.
className
=
"js-hypothesis-config"
;
config
.
className
=
'js-hypothesis-config'
;
config
.
type
=
"application/json"
;
config
.
type
=
'application/json'
;
config
.
innerText
=
'
{"constructor": "Guest"
}'
;
config
.
innerText
=
'
{"enableMultiFrameSupport": true, "subFrameInstance": true
}'
;
// THESIS TODO: Temporarily hardcoded
var
src
=
scriptUrl
;
var
src
=
'http://localhost:3001/hypothesis'
;
var
embed
=
document
.
createElement
(
'script'
);
var
embed
=
document
.
createElement
(
'script'
);
embed
.
async
=
true
;
embed
.
async
=
true
;
embed
.
src
=
src
;
embed
.
src
=
src
;
iframe
.
contentDocument
.
body
.
appendChild
(
config
);
iframe
.
contentDocument
.
body
.
appendChild
(
config
);
iframe
.
contentDocument
.
body
.
appendChild
(
embed
);
iframe
.
contentDocument
.
body
.
appendChild
(
embed
);
if
(
iframes
&&
i
<
iframes
.
length
-
1
)
{
this
.
_injectHypothesis
(
iframes
,
++
i
);
}
}
}
// Check if we can access this iframe's document
// Check if we can access this iframe's document
function
isAccessible
(
iframe
)
{
function
isAccessible
(
iframe
)
{
try
{
try
{
iframe
.
contentDocument
;
return
!!
iframe
.
contentDocument
;
return
true
;
}
catch
(
e
)
{
}
catch
(
e
)
{
return
false
;
return
false
;
}
}
}
}
// Check if this is an iframe that we want to inject embed.js into
// Check if this is an iframe that we want to inject embed.js into
function
isValid
(
iframe
)
{
function
isValid
(
iframe
)
{
// Currently only checks if it's not the h-sidebar
// Currently only checks if it's not the h-sidebar
return
(
iframe
.
className
!==
'h-sidebar-iframe'
)
;
return
iframe
.
className
!==
'h-sidebar-iframe'
;
}
}
module
.
exports
=
{
findFrames
:
findFrames
,
hasHypothesis
:
hasHypothesis
,
injectHypothesis
:
injectHypothesis
,
isAccessible
:
isAccessible
,
isValid
:
isValid
,
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment